【发布时间】:2015-04-11 13:09:08
【问题描述】:
编辑: 添加了一个 else 语句以删除复选标记。但是,现在,它不再重复复选标记,但是如果标记了两个或多个单元格并且我向下滚动并向上滚动,它将删除除一个之外的所有单元格。任何想法为什么?当这种情况发生时,我很难理解细胞回收。
我一直在努力解决这个问题,因为我无法弄清楚。任何指针或帮助将不胜感激。
每次我选择一个单元格并滚动时,它都会随机重复复选标记。我正在通过使用包含 indexPath.row + 1 的数组检查 indexPath.row 来决定单元格是否需要复选标记。
滚动时更新标题和描述效果很好,只是选中标记起作用了。
这是我当前的 cellForRowAtIndexPath 代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"KnuterItem";
if([s.taskArray valueForKey:@"TaskData"] != [NSNull null])
{
KJBasicCell *cell = (KJBasicCell*)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
// Add utility buttons
NSMutableArray *rightUtilityButtons = [NSMutableArray new];
[rightUtilityButtons sw_addUtilityButtonWithColor:
[UIColor colorWithRed:1.0f green:0.231f blue:0.188 alpha:1.0f]
title:@"Les mer"];
cell.rightUtilityButtons = rightUtilityButtons;
cell.delegate = self;
if (cell == nil) {
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"KnuterItem" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
NSDictionary *item = [s.taskArray objectAtIndex:indexPath.row];
if([item objectForKey:@"Title"] != [NSNull null])
cell.titleLabel.text = [item objectForKey:@"Title"];
if(s.textColor)
cell.titleLabel.textColor = s.textColor;
if([item objectForKey:@"Description"] != [NSNull null]){
if (s.isNotAutoRowHeight == 0) {
cell.descLabel.numberOfLines = 0;
} else {
cell.descLabel.numberOfLines = 1;
}
cell.descLabel.text = [item objectForKey:@"Description"];
if(s.textColor)
cell.descLabel.textColor = s.textColor;
cell.descriptionText = [item objectForKey:@"Description"];
}
if([[s.selfArray valueForKey:@"CheckStat"] isEqualToString:@"(null)"])
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else if([[s.selfArray valueForKey:@"CheckStat"] length] == 0)
{
if ([[item objectForKey:@"CheckStat"] containsString:@"1"])
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
NSLog(@"array: %@", [s.selfArray valueForKey:@"CheckStat"]);
for(int i = 0; i < [[[s.selfArray valueForKey:@"CheckStat"] componentsSeparatedByString:@","] count]; i++)
{
NSString *checkedNumStr = [[[s.selfArray valueForKey:@"CheckStat"] componentsSeparatedByString:@","] objectAtIndex:i];
if ([checkedNumStr intValue] == indexPath.row + 1)
cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
cell.accessoryType = UITableViewCellAccessoryNone;
}
}
return cell;
}
else
return nil;
}
didSelectRowAtIndex:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(cell.accessoryType == UITableViewCellAccessoryCheckmark)
{
cell.accessoryType = UITableViewCellAccessoryNone;
}
else
{
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
CheckStatStr = nil;
for(int i = 0; i < [s.taskArray count]; i++)
{
NSIndexPath *eachPath = [NSIndexPath indexPathForRow:i inSection:0] ;
UITableViewCell *eachCell = [tableView cellForRowAtIndexPath:eachPath];
if(eachCell.accessoryType == UITableViewCellAccessoryCheckmark)
{
if([CheckStatStr length] == 0 || !CheckStatStr)
{
CheckStatStr = [[NSMutableString alloc] init];
[CheckStatStr setString:(NSString*)[NSString stringWithFormat:@"%d",i+1]];
}
else
[CheckStatStr appendString:[NSString stringWithFormat:@",%d",i+1]];
}
}
if(CheckStatStr){
s.OldCheckStatStr = [s.selfArray valueForKey:@"CheckStat"];
[s.selfArray setValue:CheckStatStr forKey:@"CheckStat"];
}else{
[s.selfArray setValue:@"0" forKey:@"CheckStat"];
}
}
【问题讨论】:
-
你能分享一个你的数据源的例子吗?
-
数据源是一个用 json 填充的 NSMutableArray,因为我是从 MySQL 数据库中查询它。数组存储信息如下:CheckStat = 1,5,8,9,15;
标签: ios objective-c uitableview accessorytype