Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/23.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
Ios UITableView中的多个(额外)附件复选标记_Ios_Objective C_Uitableview - Fatal编程技术网

Ios UITableView中的多个(额外)附件复选标记

Ios UITableView中的多个(额外)附件复选标记,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个带有UITableView的UIViewController,其中实现了多个附件复选标记。我的问题是,当我单击tableview中的某些单元格时,它会被选中,但下面还会有一些其他单元格被选中。我可以在向下滚动tableview时查看它。我只想检查用户单击的单元格,而不是多余的单元格。请告诉我怎么做。谢谢 代码如下: - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1

我有一个带有UITableView的UIViewController,其中实现了多个附件复选标记。我的问题是,当我单击tableview中的某些单元格时,它会被选中,但下面还会有一些其他单元格被选中。我可以在向下滚动tableview时查看它。我只想检查用户单击的单元格,而不是多余的单元格。请告诉我怎么做。谢谢

代码如下:

   - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return [someData count];
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
        cell.textLabel.text=[self.someData objectAtIndex:indexPath.row];
        cell.textLabel.font=[UIFont fontWithName:@"Times New Roman" size:11];
        cell.textLabel.lineBreakMode = NSLineBreakByWordWrapping;
        [cell.textLabel setNumberOfLines:2];
        return cell;
    }

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];

        if ([selectedCell accessoryType] == UITableViewCellAccessoryNone) {
            [selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];    
            NSArray *arrayOfIndexPathsTableOne = [self.tableView indexPathsForSelectedRows];
            for (int i = 0; i < [arrayOfIndexPathsTableOne count]; i++) {
                NSIndexPath *indexPathImInterestedIn = [arrayOfIndexPathsTableOne objectAtIndex:i];
                UITableViewCell *currentCell = [self.tableView cellForRowAtIndexPath:indexPathImInterestedIn];
                [saveData addObject:[NSString stringWithFormat:@"%@", currentCell.textLabel.text]];
            }
         } else {
                [selectedCell setAccessoryType:UITableViewCellAccessoryNone];
                NSArray *arrayOfIndexPathsTableOne = [self.tableView indexPathsForSelectedRows];

                for(int i = 0; i < [arrayOfIndexPathsTableOne count]; i++) {
                    NSIndexPath *indexPathImInterestedIn = [arrayOfIndexPathsTableOne objectAtIndex:i];
                    UITableViewCell *currentCell = [self.tableView cellForRowAtIndexPath:indexPathImInterestedIn];
                    [saveData removeObject:[NSString stringWithFormat:@"%@", currentCell.textLabel.text]]; 
                }
         }
         [tableView deselectRowAtIndexPath:indexPath animated:NO];
    }
-(NSInteger)节数表视图:(UITableView*)表视图{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回[某些数据计数];
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
[self.tableView注册表类:[UITableViewCell类]强制重用标识符:@“Cell”];
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@“cell”forIndexPath:indexPath];
cell.textlab.text=[self.someData objectAtIndex:indexath.row];
cell.textlab.font=[UIFont fontWithName:@“Times New Roman”大小:11];
cell.textlab.lineBreakMode=NSLineBreakByWordWrapping;
[cell.textlab setNumberOfLines:2];
返回单元;
}
-(void)tableView:(UITableView*)tableView未选择RowatineXpath:(NSIndexPath*)indexPath{
UITableViewCell*selectedCell=[tableView cellForRowAtIndexPath:indexPath];
if([selectedCell accessoryType]==UITableViewCellAccessoryNone){
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
NSArray*arrayOfIndexPathsTableOne=[self.tableView IndExpathForSelectedRows];
for(int i=0;i<[ArrayFindExpathStableon计数];i++){
nsindepath*indexpatiminterestedin=[ArrayFindExathPathStableon对象索引:i];
UITableViewCell*currentCell=[self.tableView cellForRowAtIndexPath:IndExpathineTrestedIn];
[saveData addObject:[NSString stringWithFormat:@“%@”,currentCell.textLabel.text];
}
}否则{
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
NSArray*arrayOfIndexPathsTableOne=[self.tableView IndExpathForSelectedRows];
for(int i=0;i<[ArrayFindExpathStableon计数];i++){
nsindepath*indexpatiminterestedin=[ArrayFindExathPathStableon对象索引:i];
UITableViewCell*currentCell=[self.tableView cellForRowAtIndexPath:IndExpathineTrestedIn];
[saveData removeObject:[NSString stringWithFormat:@“%@”,currentCell.textLabel.text];
}
}
[tableView取消行索引路径:索引路径:否];
}

这是一个单元重用问题


要修复此问题,请确保每次从
cellforrowatinexpath:
返回单元格时,始终设置选择(复选标记)状态。这意味着要根据需要显式地将其设置为true和false。

您只需要两件事:

  • UITableView
    上禁用多选功能
  • UITableViewCell
    s中执行
    setSelected:(BOOL)selected
    ,并相应地选择/取消选择单元格的子视图(复选标记)

  • 当您选择table时,会在其上显示复选框,但当您滚动时,单元格会消失,并在您向后滚动后进入可重用池,单元格会从此池中取出,并且不记得“状态”


    解决方案是创建NSMutableArray,并在didSelectCellAtIndexPath中:方法将该单元格的indexPath添加或删除到数组中,在cellForRowAtIndexPath中:检查此数组并在该表的基础上显示/隐藏复选标记。

    不要在单元格中为索引路径处的行写入此行

    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
    

    它必须在loadview或viewdidload中

    我已经测试了您的代码,我发现问题在于UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@“cell”forIndexPath:indexPath];因为您是可重用单元的出列者,这就是它再次创建的原因。你需要静态电池。每行一个单元格对象。如下所示:-UITableViewCell*cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:@“cell”];