xcode 在表格视图中使用附件复选标记

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6981952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:41:16  来源:igfitidea点击:

Using accessory check mark in a table view

iphoneobjective-cxcodeuitableview

提问by sujay

I have a array of 8 items put in a table view.I am using the following code to set the check mark on tap of a row

我在表格视图中放置了一个包含 8 个项目的数组。我正在使用以下代码在点击一行时设置复选标记

[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];

[[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];

.But my problem is 1] whenever i tap my first row ,the fifth rows check mark also get showed up. And 2] when i scroll the table view after checking a row , and return back , checked mark gets disappears. How to get rid of these problem. thanks in advance

.但我的问题是 1] 每当我点击我的第一行时,第五行复选标记也会出现。和 2] 当我在检查一行后滚动表格视图并返回时,选中的标记消失。如何摆脱这些问题。提前致谢

here is the code

这是代码

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {

    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    // find the cell being touched and update its checked/unchecked image
   CellView *targetCustomCell = (CellView *)[tableView cellForRowAtIndexPath:indexPath];
    [targetCustomCell checkAction:nil];

    if ([arrData count]>0) 
    {
        if ([arrData containsObject:[arrName objectAtIndex:indexPath.row]]) {
            [arrData removeObject:[arrName objectAtIndex:indexPath.row]];
            [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryNone];   
            NSLog(@"data removed");
            NSLog(@"arrData%@",arrData);
        }
        else {
            [arrData addObject:[arrName objectAtIndex:indexPath.row]];
            [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];   
            NSLog(@"data added");
            NSLog(@"tableArray%@",arrData);
        }

    }
    else {
        [arrData addObject:[arrName objectAtIndex:indexPath.row]];
        [[tableView cellForRowAtIndexPath:indexPath] setAccessoryType:UITableViewCellAccessoryCheckmark];   
        NSLog(@"data added");
        NSLog(@"arrData%@",arrData);
    }
   } 

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *kCustomCellID = @"MyCellID";

        CellView *cell = (CellView *)[tableView dequeueReusableCellWithIdentifier:kCustomCellID];
        if (cell == nil)
        {
        cell = (CellView *)[[[CellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
        }
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont boldSystemFontOfSize:13];  
        cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.textLabel.textAlignment = UITextAlignmentLeft;
        cell.textLabel.text = [arrName objectAtIndex:indexPath.row];
        return cell;
    }

回答by Maulik

for your 1) problem you have provide some code

对于您的 1) 问题,您提供了一些代码

2) problem :

2)问题:

you need to remember your last selected row for that you need to store your last selected index number by :

您需要记住上次选择的行,因为您需要通过以下方式存储上次选择的索引号:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
    selectedIndex = indexPath.row;
    [tableView reloadData]; 
}

and in your

并且在你的

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
   if (indexPath.row == selectedIndex) 
  {
     cell.accessoryType = UITableViewCellAccessoryCheckmark;
  }
     cell.accessoryType = UITableViewCellAccessoryNone;

    return cell;
}

hope it helps you.

希望对你有帮助。