我已经使用了UITableView。我想在行上使用复选标记。当选择行时,复选标记应显示在那里,当他选择复选标记行时,复选标记应该消失。 我使用了以下代码。
在我的代码中,我只选择多行,但能够在选定行的相同复选标记上消失。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//if (cell == nil) {
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.textLabel.text=[[gmailList objectAtIndex:indexPath.row]valueForKey:@"Name"];
if(cell.selected)
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
// Configure the cell...
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Uncheck the previous checked row
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.selected)
{
cell.accessoryType = UITableViewCellAccessoryNone;
cell.selected=FALSE;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.selected=TRUE;
}
}
答案 0 :(得分:3)
Try this- You have to define a global NSMutableDict and use that dict as below
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
//if (cell == nil) {
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.textLabel.text=[[gmailList objectAtIndex:indexPath.row]valueForKey:@"Name"];
if([[dict objectForKey:[NSString stringWithFormat:"%d",indexPath.row]] isEqualToString:@"YES"])
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
// Configure the cell...
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Uncheck the previous checked row
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.selected)
{
cell.accessoryType = UITableViewCellAccessoryNone;
[[dict setObject:@"NO"]] forKey:[NSString stringWithFormat:"%d",indexPath.row];
cell.selected=FALSE;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
cell.selected=TRUE;
[[dict setObject:@"YES"]] forKey:[NSString stringWithFormat:"%d",indexPath.row];
}
}
答案 1 :(得分:3)
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath*)indexPath{
if ([tableView tag]==2){
self.clearCheckMark = false;
if ([tableView cellForRowAtIndexPath:indexPath].accessoryType ==UITableViewCellAccessoryCheckmark)
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryNone;
}
else
{
[tableView cellForRowAtIndexPath:indexPath].accessoryType = UITableViewCellAccessoryCheckmark;
}
self.index = [tableView indexPathsForSelectedRows];
}
return indexPath;}
答案 2 :(得分:0)
使用" isChecked"等键设置NSMutableDictionary。 "是"或"否"使用以下方法
- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
NSMutableDictionary *dic = [gmailList objectAtIndex:indexPath.row];
if([[dic valueForKey:@"isChecked"]boolValue])
{
cell.accessoryType = UITableViewCellAccessoryNone;
[dic setObject:@"NO" forKey:@"isChecked"];
cell.selected=FALSE;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
[dic setObject:@"YES" forKey:@"isChecked"];
cell.selected=TRUE;
}
[self.tableView reloadData];
}