我的didSelectRowAtIndexPath:方法中有以下代码,我希望用户能够点击一个单元格,让单元格显示复选标记,并将选定的单元格添加到单独的数组中。我还希望用户能够点击已经选中的单元格,然后取出复选标记并将其从阵列中删除。
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.setupTable deselectRowAtIndexPath:indexPath animated:YES];
SetupCell *cell = (SetupCell*)[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"Feed name: %@", cell.setupFeedName.text);
if ([self.selectedCells containsObject:cell.setupFeedName.text]) {
[self.selectedCells removeObjectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
[self.selectedCells addObject:cell.setupFeedName.text];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
[self.setupTable reloadData];
NSLog(@"%i cells are selected.", [self.selectedCells count]);
NSLog(@"%i cells are not selected.", [self.setupFeeds count]);
}
NSLog输出以下内容:
2013-08-12 07:04:58.767 [13921:c07] Feed name: Politico
2013-08-12 07:04:58.768 [13921:c07] 0 cells are selected.
2013-08-12 07:04:58.769 [13921:c07] 6 cells are not selected.
更新
以下是控制台显示的内容:
2013-08-12 07:31:40.390[14117:c07] Feed name: Huffington Post
2013-08-12 07:31:40.392[14117:c07] 5 cells are selected.
2013-08-12 07:31:40.392[14117:c07] 6 cells are not selected.
以下是代码:
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self.setupTable deselectRowAtIndexPath:indexPath animated:YES];
SetupCell *cell = (SetupCell*)[tableView cellForRowAtIndexPath:indexPath];
NSLog(@"Feed name: %@", cell.setupFeedName.text);
if ([[self.selectedCells objectAtIndex:indexPath.row] isEqualToString:cell.setupFeedName.text]) {
[self.selectedCells removeObjectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
[self.selectedCells addObject:cell.setupFeedName.text];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
NSLog(@"%i cells are selected.", [self.selectedCells count]);
NSLog(@"%i cells are not selected.", [self.setupFeeds count]);
[self.setupTable reloadData];
}
答案 0 :(得分:0)
我在下面的方式做了这个,但我在单元格上取了自定义按钮来检查取消选中。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell * cell =
[tableHistory dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIdentifier] autorelease];
}
else
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:SimpleTableIdentifier] autorelease];
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(270.0, 7.0, 30.0, 30.0)];
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"check"])
[button setImage:[UIImage imageNamed:@"right-with-bg.png"] forState:UIControlStateNormal];
else
[button setImage:[UIImage imageNamed:@"tick-bg.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonClicked:)forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:button];
cell.textLabel.text = [cellDataArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [arrayTktcode objectAtIndex:indexPath.row];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
[cell addSubview:btnUnScan];
[cell addSubview:btnUScan];
return cell;
}
-(void)buttonClicked:(id)sender
{
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:tableHistory];
NSIndexPath *indexPath = [tableHistory indexPathForRowAtPoint:touchPoint];
UIButton *button = (UIButton *)sender;
if([[arrayCheckUnchek objectAtIndex:indexPath.row] isEqualToString:@"Uncheck"])
{
[button setImage:[UIImage imageNamed:@"right-with-bg.png"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"check"];
}
else
{
[button setImage:[UIImage imageNamed:@"tick-bg.png"] forState:UIControlStateNormal];
[arrayCheckUnchek replaceObjectAtIndex:indexPath.row withObject:@"Uncheck"];
}
}
检查this also。
答案 1 :(得分:0)
您需要创建一个存储所选单元格的数组。并保持所选单元格的轨迹。如果再次点击单元格,则从阵列中删除该特定单元格。
从中获取详细信息 example given
答案 2 :(得分:0)
// use this in didSelectRow
NSUInteger index = [self.selectedCells indexOfObject:cell.setupFeedName.text];
if (index != NSNotFound) {
[self.selectedCells removeObjectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
[self.selectedCells addObject:cell.setupFeedName.text];
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}