【问题标题】:When i scrolling on UITableview,the checkmark of multiple selected rows are disappears.how to solve this?当我在 UITableview 上滚动时,多个选定行的复选标记消失了。如何解决这个问题?
【发布时间】:2017-07-27 04:32:23
【问题描述】:

我想在 UITableview 中实现多行选择。但是当我选择带有复选标记的多行并向下滚动时,当我返回所选行部分时,复选标记会消失。我已经尝试了 Stackoverflow 的许多解决方案,但它没有为我工作。请任何人给我适合我的解决方案。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {

        static NSString *simpleTableIdentifier = @"SimpleTableCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

        if (cell == nil)
        {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        }
            if([_selectedstatearray containsObject:indexPath]) {
                cell.accessoryType = UITableViewCellAccessoryCheckmark;
            }
            else {
                cell.accessoryType = UITableViewCellAccessoryNone;

            }


        cell.textLabel.text = [statearray objectAtIndex:indexPath.row];
        return cell;

    }


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

        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        NSString *cellText = cell.textLabel.text;
        NSLog(@"cellText>>%@",cellText);

        if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
            [_selectedstatearray removeObject:cellText];
            cell.accessoryType = UITableViewCellAccessoryNone;

        } else {
            [_selectedstatearray addObject:cellText];
            cell.accessoryType=UITableViewCellAccessoryCheckmark;
        }

        NSLog(@"selectedarray>>%@",_selectedstatearray);

        NSString *greeting = [_selectedstatearray componentsJoinedByString:@","];
        NSLog(@"%@",greeting);


        [tableView deselectRowAtIndexPath:indexPath animated:YES];


    }

【问题讨论】:

标签: ios objective-c uitableview row multipleselection


【解决方案1】:

问题出在这段代码上:

if([_selectedstatearray containsObject:indexPath]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

您正在检查 _selectedstatearray 是否包含指定的 indexPath,同时您将它们存储为 NSString [_selectedstatearray addObject:cellText];

将上面的代码替换为:

NSString *text = [statearray objectAtIndex:indexPath.row];
if([_selectedstatearray containsObject:text]) {
    cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
    cell.accessoryType = UITableViewCellAccessoryNone;
}

【讨论】:

  • 非常感谢。它的工作原理。以及如何在其他屏幕上获取这些选定的行?
  • @swapnilpatel 您必须维护数组。每当用户选择任何行时,您都会获得该行的 indexPath,因此将 indexPath.row 存储在数组中并将该数组传递给您想要选择行的那个 VC。
  • 如果您希望此 tableView 在选择行时通知其他视图控制器,您可以创建自定义协议并将委托设置为该视图控制器,也可以使用块。如果您只想从另一个视图控制器访问选定的行,您可以简单地将_selectedstatearray 声明为.h 文件中的属性。
  • selectedstatearray 我已经定义为属性。然后呢?我必须将这些数组传递给另一个视图控制器?
  • 如果它已经是视图控制器A的一个属性,你可以通过调用viewcontrollerA.selectedstatearray从另一个视图控制器(视图控制器B)简单地访问它
【解决方案2】:

回答您最后的评论

如果您将 selectedstatearray 传递给另一个 ViewController's (viewControllerB) UITableView,它只包含 selectedstatearray 那么你可以添加

cell.accessoryType = UITableViewCellAccessoryCheckmark

在你的 UITableVIew 委托方法

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 

这将为所有行添加复选标记

希望对你有帮助....

【讨论】:

    猜你喜欢
    • 2015-03-11
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 2017-11-06
    • 1970-01-01
    相关资源
    最近更新 更多