1

我正在使用以下代码向 UITableViewCellAccessoryCheckmark 添加自定义图标。问题是当触摸/选择另一行时,单元格复选标记不会取消选择

这是我的代码:

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

    int index = indexPath.row; id obj = [listOfItems objectAtIndex:index];


    //This toggles the checkmark
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];


    if (cell.accessoryType == UITableViewCellAccessoryNone)
    {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;

        UIImage *image = [UIImage imageNamed:@"icon-tick.png"];

        UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];

        [downloadButton setImage:image forState:UIControlStateNormal];
        [downloadButton setFrame:CGRectMake(0, 0, 19, 19)];
        [downloadButton setBackgroundColor:[UIColor clearColor]];
        [self.tableView cellForRowAtIndexPath:indexPath].accessoryView = downloadButton;

        //This sets the array

    } else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;

        UIButton *downloadButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [downloadButton setTitle:@"" forState:UIControlStateNormal];
        [downloadButton setFrame:CGRectMake(0, 0, 0, 0)];
        [self.tableView cellForRowAtIndexPath:indexPath].accessoryView = downloadButton;

    }

    NSMutableData *data = [NSMutableData data];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    // Customize archiver here
    [archiver encodeObject:obj forKey:@"keyForYourArrayOfNSIndexPathObjects"];
    [archiver finishEncoding];
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"keyForYourArrayOfNSIndexPathObjects"];


    NSKeyedUnarchiver *unarchiver;
    unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:
                  [[NSUserDefaults standardUserDefaults] objectForKey:@"keyForYourArrayOfNSIndexPathObjects"]];
    // Customize unarchiver here
    countryItemSelected = [unarchiver decodeObjectForKey:@"keyForYourArrayOfNSIndexPathObjects"];
    [unarchiver finishDecoding];

    NSLog(@"list of items %@", countryItemSelected);

    // Navigation logic may go here. Create and push another view controller.

     CitiesViewController *detailViewController = [[CitiesViewController alloc] init];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];

}

谢谢你的帮助

4

3 回答 3

2

“复选标记没有取消选择”到底是什么意思?无论如何:当取消选择单元格时,您可以使用委托方法tableView:didDeselectRowAtIndexPath:并将附件更改为不同的东西。

于 2013-01-17T11:40:57.153 回答
0

试试下面的代码

int rowNumber = [tableCategory numberOfRowsInSection:0];

for (int indexRow = 0 ; indexRow < rowNumber; indexRow++)
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:indexRow inSection:0];

    UITableViewCell *cell = [tableCategory cellForRowAtIndexPath:indexPath];

    if (indexPath.row == <required indexpath row>)
    {
        //do what you want with cell
    }
    else
    {
        //do what you want with cell
    }
}

希望它可以帮助你

于 2013-01-17T12:07:19.687 回答
0

我刚刚做对了,希望这对你们有帮助!

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


for(int i=0; i<[tableView numberOfRowsInSection:0]; i++){
    NSIndexPath *index = [NSIndexPath indexPathForRow:i inSection:0];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:index];

    if(index.row == indexPath.row){
        NSLog(@"select");
    }else{
        NSLog(@"not select");
    } 
} 

}

于 2015-09-16T01:56:12.730 回答