Objective c 如何使用UITableViewCellAccessoryCheckmark检查uitableview中的多行
大家好,我知道这是一个常见的问题,但请帮助我。我有一个tableview,在其中显示NSArray中的数据。我想用UITableViewCellAccessoryCheckmark选择多行。但当我滚动时,复选标记消失了。每个人都在谈论可重复使用的细胞。我理解这个概念,但无法解决我的问题。所以请有人能给我实现CellForRowatineXpath和DidSelectRowatineXpath的方法谢谢。很紧急,我用这个密码Objective c 如何使用UITableViewCellAccessoryCheckmark检查uitableview中的多行,objective-c,uitableview,Objective C,Uitableview,大家好,我知道这是一个常见的问题,但请帮助我。我有一个tableview,在其中显示NSArray中的数据。我想用UITableViewCellAccessoryCheckmark选择多行。但当我滚动时,复选标记消失了。每个人都在谈论可重复使用的细胞。我理解这个概念,但无法解决我的问题。所以请有人能给我实现CellForRowatineXpath和DidSelectRowatineXpath的方法谢谢。很紧急,我用这个密码 -(UITableViewCell *)tableView:(UITab
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *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;
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
UITableViewCell*单元=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:@“单元”];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1重用标识符:@“cell”];
}
cell.textLabel.text=[listData objectAtIndex:indexPath.row];
[cell setAccessoryType:UITableViewCellAccessoryNone];
for(int i=0;i
如果我正确理解了您的问题,那么您正在创建一个数组,并在选择行时将索引添加到该数组中。这是行不通的。
我建议您使用字典来映射选定的行
NSMutableDictionary *selectedIndexDict;
在您的didselectrowatinexpath:
方法中
[selectedIndexDict setValue:[NSNumber numberWithBool:YES] forKey:[NSString stringWithFormat:@"%d", indexPath.row]];
[cell setAccessoryType:UITableViewCellAccessoryNone];
if ([[selectedIndexDict valueForKey:[NSString stringWithFormat:@"%d", indexPath.row]] boolValue]) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
并且,在您的cellforrowatinexpath:
方法中
[selectedIndexDict setValue:[NSNumber numberWithBool:YES] forKey:[NSString stringWithFormat:@"%d", indexPath.row]];
[cell setAccessoryType:UITableViewCellAccessoryNone];
if ([[selectedIndexDict valueForKey:[NSString stringWithFormat:@"%d", indexPath.row]] boolValue]) {
[cell setAccessoryType:UITableViewCellAccessoryCheckmark];
}
谢谢你,它真的很有帮助,而且很快就编码了:)再次感谢