这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
//custom code
[self.table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
//add table
....
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//a method that add context for cell, return void
[self cellView:cell withInfo:[self.arrayTaxiList objectAtIndex:indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"cell: %@ -------- indexpath: %d", cell, indexPath.row);
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
问题是滚动时复选标记会重新出现在另一个单元格上。 这是NSLog的结果:
cell: <UITableViewCell: 0x16f37a60; frame = (0 0; 320 50); text = ' Hoàng Anh'; autoresize = W; layer = <CALayer: 0x16f33f80>> -------- indexpath: 0
cell: <UITableViewCell: 0x16f37a60; frame = (0 450; 320 50); text = '84'; autoresize = W; layer = <CALayer: 0x16f33f80>> -------- indexpath: 9
cell: <UITableViewCell: 0x16f37a60; frame = (0 900; 320 50); text = 'Ân Thi'; autoresize = W; layer = <CALayer: 0x16f33f80>> -------- indexpath: 18
与单元格1 - 10 - 20或2 - 11 - 21 ...
相同的问题如您所见,单元格的地址没有改变。这就是复选标记重新出现的原因。
如何摆脱这个问题?
感谢。
答案 0 :(得分:1)
@property(nonatomic, strong)NSMutableDictionary *selectionDetails;
-(NSMutableDictionary)selectionDetails{
if(!_selectionDetails)
_selectionDetails = [[NSMutableDictionary alloc]init];
return _selectionDetails;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
**if([self.selectionDetails objectForKey:@"accessoryType"]){
NSString *status = [self.selectionDetails objectForKey:@"accessoryType"];
if([status isEqualToString:@"checked"])
[self.selectionDetails setObject:@"checked" forKey:@"accessoryType"];
else
[self.selectionDetails setObject:@"none" forKey:@"accessoryType"]
}else{
[self.selectionDetails setObject:@"none" forKey:@"accessoryType"]
}**
//a method that add context for cell, return void
[self cellView:cell withInfo:[self.arrayTaxiList objectAtIndex:indexPath.row]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"cell: %@ -------- indexpath: %d", cell, indexPath.row);
if (cell.accessoryType == UITableViewCellAccessoryNone) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
**[self.selectionDetails setObject:@"checked" forKey:@"accessoryType"];**
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
**[self.selectionDetails setObject:@"none" forKey:@"accessoryType"];**
}
}
答案 1 :(得分:1)
试试这个,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.table dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
NSDictionary *taxiDictionary = [self.arrayTaxiList objectAtIndex:indexPath.row];
if ([[taxiDictionary valueForKey:@"accessoryType"] isEqualToString:@"none"]) {
cell.accessoryType = UITableViewCellAccessoryNone;
} else {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
//a method that add context for cell, return void
[self cellView:cell withInfo:taxiDictionary];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
NSLog(@"cell: %@ -------- indexpath: %d", cell, indexPath.row);
NSMutableDictionary *taxiDictionary = [NSMutableDictionary dictionaryWithDictionary:[self.arrayTaxiList objectAtIndex:indexPath.row]];
if (cell.accessoryType == UITableViewCellAccessoryNone) {
[taxiDictionary setValue:@"checked" forKey:@"accessoryType"];
} else {
[taxiDictionary setValue:@"none" forKey:@"accessoryType"]
}
[tableView beginUpdates];
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView endUpdates];
}
答案 2 :(得分:0)
这一切都发生在这行代码中
//a method that add context for cell, return void
[self cellView:cell withInfo:[self.arrayTaxiList objectAtIndex:indexPath.row]];
-(void)cellView:(UITableViewCell *)cell withInfo:(NSDictionary *)info {}
此方法接收UITableViewCell
并编辑其上下文。因此,这就是为什么上面的NSLog
会在内存中返回一个具有相同地址的UITableViewCell
。
我把它改成了
cell = [self cellViewWithInfo:[self.arrayTaxiList objectAtIndex:indexPath.row]];
-(UITableViewCell *)cellViewWithInfo:(NSDictionary *)info {}
现在它的工作方式应该如此。
谢谢各位回复。