Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 选择单元格并将其移动到单独的阵列?_Iphone_Uitableview_Sdk_Nsmutablearray - Fatal编程技术网

Iphone 选择单元格并将其移动到单独的阵列?

Iphone 选择单元格并将其移动到单独的阵列?,iphone,uitableview,sdk,nsmutablearray,Iphone,Uitableview,Sdk,Nsmutablearray,我的didSelectRowAtIndexPath:method中有以下代码,我希望用户能够点击单元格,让单元格显示复选标记,并将所选单元格添加到单独的数组中。我还希望用户能够点击一个已经选定的单元格,然后去掉复选标记并将其从数组中删除 -(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self.setupTable deselectRowAtInd

我的didSelectRowAtIndexPath:method中有以下代码,我希望用户能够点击单元格,让单元格显示复选标记,并将所选单元格添加到单独的数组中。我还希望用户能够点击一个已经选定的单元格,然后去掉复选标记并将其从数组中删除

-(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.
更新:

以下是Console显示的内容:

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

}

我是用下面的方法做的,但我用了单元格上的自定义按钮来勾选取消勾选

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

选中。

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


重新加载之前,请尝试此NSLog。到底是什么问题?那么在你的情况下会发生什么?你是在初始化self.selectedCells吗?如果是,那么在哪里?@HinataHyuga,让我现在试试。@Ashwin,很抱歉不清楚。当前代码将选择单元格并显示复选标记,但不会取消选择并删除复选标记。嗯。。。这就是我想做的。请仔细看看我的代码。我正在尝试将所选单元格添加到一个单独的数组中。请查看示例链接,您将获得解决方案。如果成功!但是我怎样才能在控制台中打印出有多少选定单元格和有多少未选定单元格?您将从DIDSELECTROWATINDEXPATH获得它。您能告诉我您到底想要什么吗?我仍然没有正确地满足您的要求?
   // 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;
    }