0

我的 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];

}
4

3 回答 3

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"];
}
}

也检查一下

于 2013-08-12T12:26:59.610 回答
0

您需要创建一个存储所选单元格的数组。并跟踪选定的单元格。如果再次点击单元格,则从数组中删除该特定单元格。

从给出的示例中获取详细信息

于 2013-08-12T12:30:27.073 回答
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;
    }
于 2013-08-12T12:49:29.823 回答