在tableview中选择多行

时间:2012-10-31 06:36:59

标签: iphone uitableview

我在iPhone应用程序中实现了UITableView。更进一步,我在UITableView中添加了多行选择。但问题是,当我选择特定行然后滚动表并返回时,未选中(未标记)的相应行。这是我的didSelectRowAtIndexPath方法。

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

        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        if([tableView cellForRowAtIndexPath:indexPath].accessoryType==UITableViewCellAccessoryNone)
        {  
            [tableView cellForRowAtIndexPath:indexPath].accessoryType=UITableViewCellAccessoryCheckmark;        
            [selectedIndexes addObject:[NSNumber numberWithInt:indexPath.row]];
            NSLog(@"Select now %d",[selectedIndexes count]);
        }
        else if([tableView cellForRowAtIndexPath:indexPath].accessoryType==UITableViewCellAccessoryCheckmark)
        {
            [tableView cellForRowAtIndexPath:in dexPath].accessoryType=UITableViewCellAccessoryNone;
            [selectedIndexes removeObject:[NSNumber numberWithInt:indexPath.row]];
            NSLog(@"DeSelect now %d",[selectedIndexes count]);
        }   
}

请给我解决方案。

2 个答案:

答案 0 :(得分:1)

每次滚动表视图时,每次调用委托方法cellForRowAtIndexPath方法。因此,它每次都会加载数据。然后,首先必须存储在didSelectRowAtIndexPath方法中单击哪些行,以及根据cellForRowAtIndexPath方法中的这些行索引,您必须显示已使用复选标记选择了哪些表视图行以及没有复选标记的剩余行...

答案 1 :(得分:0)

检查一下。 multiple-row-selection-and-editing

你会发现真的很容易;是有用的。

享受编程

相关问题