【发布时间】:2015-02-26 07:08:30
【问题描述】:
我是 iOS 开发的新手,我制作了一个包含 UITableView 的应用程序我像一样编码但总是设置标签等于1我想要如果选择了单元格然后标签是1否则标签等于0请给我解决方案。
这里是我的代码
在viewDidLoad
NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
SelectedRows = [NSMutableArray arrayWithArray:[userDef objectForKey:@"SelectedRows"]];
SelectedRows 是NSMutableArray
-(CategoryCustumCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=@"Cell";
CategoryCustumCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib=[[NSBundle mainBundle]loadNibNamed:@"CategoryCustumCell" owner:self options:nil];
cell=[nib objectAtIndex:0];
}
cell.categoryLabel.text=[self.categoryArray objectAtIndex:indexPath.section];
NSNumber *obj = [NSNumber numberWithInteger:indexPath.section];
if ([SelectedRows containsObject:obj])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.tag=1;
intValue=cell.tag;
NSLog(@"Tag %d",intValue);
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.tag=0;
NSLog(@"Tag %d",cell.tag);
}
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
CategoryCustumCell *cell = (CategoryCustumCell *)[tableView cellForRowAtIndexPath:indexPath];
NSString *selectedCell= cell.categoryLabel.text;
NSLog(@"Selected Cell Value %@",selectedCell);
NSNumber *obj = [NSNumber numberWithInteger:indexPath.section];
if ([SelectedRows containsObject:obj])
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
[SelectedRows removeObject:obj];
[tableView reloadData];
}else{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
[SelectedRows addObject:obj];
[tableView reloadData];
}
NSUserDefaults *userDef = [NSUserDefaults standardUserDefaults];
[userDef setObject:SelectedRows forKey:@"SelectedRows"];
[userDef synchronize];
}
并且我将这个标记值传递到 Url Like as
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:@"http://www.janvajevu.com/webservice/insert_gcm.php?gcm_id=%@& category1=%d&category2=%d&category3=%d&category4=%d&category5 =%d&category6=%d&category7 =%d&category8=%d& category9=%d",uniqueIdentifier,intValue,intValue,intValue,intValue,intValue,intValue,intValue,intValue,intValue]];
在这里我想要标记值1 如果单元格被选中,标记值0 如果单元格未被选中请给我解决方案
【问题讨论】:
-
你为什么使用
indexPath.section?使用indexPath.row需要在当前实现中做很多改动。 -
@MidhunMP 这里我的 Tableview 包含部分所以我写了索引 path.Section。
indexpath.section或index path.row对我来说无关紧要,我只想标记值。 -
一个部分有多少行?
-
@MidhunMP 每个部分只有一行。
-
你把这个 NSURL *url=... 行放在哪里,你是如何计算 intValue 的?
标签: ios objective-c uitableview tags