滚动时,TableView上的复选标记会重复

时间:2015-04-11 13:09:08

标签: ios objective-c uitableview accessorytype

编辑: 添加了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"];
    }
}

5 个答案:

答案 0 :(得分:2)

当您选中if ([checkedNumStr intValue] == indexPath.row + 1)并设置添加复选标记时,如果不是,则需要设置cell.accessoryType = UITableViewCellAccessoryNone

if ([checkedNumStr intValue] == indexPath.row + 1)
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
else
    cell.accessoryType = UITableViewCellAccessoryNone;

表视图单元格会被重复使用,因此如果具有复选标记的单元格被重用于您不想使用复选标记的行,则需要删除复选标记。

答案 1 :(得分:0)

不看源头,我可以告诉你是细胞回收的受害者。 Apple出于性能原因执行此操作,请确保取消选中tableView:cellForRowAtIndexPath:中的单元格(删除复选标记)(如果不应选中),否则在回收其他单元格时将保留复选标记。
cell.accessoryType = UITableViewCellAccessoryNone

使用更多代码进行编辑:

- (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];
    s.taskArray[indexPath.row][@"CheckStat"] = @YES
}

答案 2 :(得分:0)

另一个原因是,如果有任何图层支持的效果(即动画)...点击CB本身并弹出&#34;查看效果检查器&#34; ..在那里你应该查看CA(核心动画)图层列表..

如果您已经玩过CB的渲染,我建议您查看并启用正确的图层,或重新连接渲染严重的版本:D

答案 3 :(得分:0)

使用Swift 3.0,

a

变为

`cell.accessoryType = UITableViewCellAccessoryType.none

答案 4 :(得分:0)

我如何通过 迅速5

解决此问题

重复选中标记的主要原因是可重复使用的单元格。然后显示单元重用并选中“仍然出现”标记。我如何解决此问题:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = self.schedulTable.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        as! scheduleCell

    cell.timeLable.text = self.timeArray[indexPath.row]
    // here is the code which clear checkmark on reuse
    if item.num >= 0 && item.isSelected == true{
        if item.num == indexPath.row{
            cell.accessoryType = .checkmark
        }else{
             cell.accessoryType = .none
        }
    }


       return cell
   }



 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
 selected_Time = self.timeArray[indexPath.row]
    item.isSelected = true
   item.num = indexPath.row
    schedulTable.cellForRow(at: indexPath)?.accessoryType = .checkmark
  //schedulTable.reloadData()

}

func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
    schedulTable.cellForRow(at: indexPath)?.accessoryType = .none
}


struct Item {
 var num : Int
 var isSelected : Bool
init() {
    num = -1
    isSelected = false
}}