开发者

how can i check multiple rows in uitableview using UITableViewCellAccessoryCheckmark

开发者 https://www.devze.com 2023-03-07 14:23 出处:网络
hi everybody i know it\'s a frequent question but please help me. i have a tableview in which i display data from an NSArray. i want to choose multiple rows with the UITableViewCellAccessoryCheckmark.

hi everybody i know it's a frequent question but please help me. i have a tableview in which i display data from an NSArray. i want to choose multiple rows with the UITableViewCellAccessoryCheckmark. but when i scroll checkmarks disappear. everybody talk about reusable cell. I understand this concept but can't resolve my problem. So please please can someone give me the implementation of cellForRowAtIndexPath and didSelectRowAtIndexPath methods thank you. It's urgent.I use this code

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    U开发者_如何学CITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"]; 

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];

    }


    cell.textLabel.text = [listData objectAtIndex:indexPath.row];


    [cell setAccessoryType:UITableViewCellAccessoryNone];
    for (int i = 0; i < selectedIndexes.count; i++) {
        NSUInteger num = [[selectedIndexes objectAtIndex:i] intValue];

        if (num == indexPath.row) {
            [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
            // Once we find a match there is no point continuing the loop
            break;
        }
    }


If I understand your problem correctly, you are having an array and adding the indexes to the array whenever the rows are selected. That won't work. I suggest you to have a dictionary to map the selected rows,

NSMutableDictionary *selectedIndexDict;

In your didSelectRowAtIndexPath: method,

[selectedIndexDict setValue:[NSNumber numberWithBool:YES] forKey:[NSString stringWithFormat:@"%d", indexPath.row]];

And, in your cellForRowAtIndexPath: method,

[cell setAccessoryType:UITableViewCellAccessoryNone];

if ([[selectedIndexDict valueForKey:[NSString stringWithFormat:@"%d", indexPath.row]] boolValue]) {

    [cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消