在编辑情绪中,表视图单元格保持未选中状态

时间:2021-09-01 22:18:58

I want even numbered rows of tableview to be selected when table is first loaded. I have set the tableview editing style to "Multiple Selection During Editing" from storyboard. I have tried two ways to do this, first approach is like below

我想要在第一次加载表时选择偶数行的tableview。我已经从storyboard将tableview编辑样式设置为“编辑期间的多个选择”。我尝试了两种方法,第一种方法如下

override func viewDidLoad() {
    super.viewDidLoad()
    self.tableView.delegate = self
    self.tableView.dataSource = self
    self.tableView.setEditing(true, animated: true)
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
    cell?.textLabel?.text = data[indexPath.row]
    if (indexPath.row % 2 == 0){
        cell?.isSelected = true
    }else {
        cell?.isSelected = false
    }
    return cell!
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
   return data.count
}

but it does not make the row selected. table looks like

但它不会选中该行。表看起来像

在编辑情绪中,表视图单元格保持未选中状态

second way i tried is to call self.tableView.selectRow(at: index path, animated: true, scrollPosition: UITableViewScrollPosition.none) whith removing additional logic in cellForRow method used in first approach. this approach seems to work.

我尝试的第二种方法是调用self.tableView.selectRow(at:index path,animated:true,scrollPosition:UITableViewScrollPosition.none),删除第一种方法中使用的cellForRow方法中的其他逻辑。这种方法似乎有效。

Can anyone tell me why first approach is not working and weather my second approach is appropriate or not. Thanks :)

任何人都可以告诉我为什么第一种方法不起作用,天气我的第二种方法是否合适。谢谢 :)

1 个解决方案

#1


0  

Check if you are doing deselectRowAtIndexPath in didSelectRowAtIndexPath method.

检查您是否在didSelectRowAtIndexPath方法中执行deselectRowAtIndexPath。

#1


0  

Check if you are doing deselectRowAtIndexPath in didSelectRowAtIndexPath method.

检查您是否在didSelectRowAtIndexPath方法中执行deselectRowAtIndexPath。