滚动

时间:2017-03-20 23:21:18

标签: ios iphone swift uitableview swift3

这似乎是一个应该足够普遍的问题,它可以在线获得一个好的答案,但我找到了最难找到的问题。

基本上,我的问题是:UITableView中有一个ViewController,此表已启用MultipleSelection。最初,如果我"检查" (即选中)单元格然后滚动直到它们不在屏幕上,当我向上滚动时复选标记消失了。我能够解决我在网上找到的第一个问题,但现在我遇到了一个新问题,必须与UITableView重复使用单元格的方式有关:当我选择单元格时,它们也不恰当地选择关闭的单元格-screen。

viewDidLoad

self.friendListTableView.allowsMultipleSelection = true

我实现了这两个功能:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}

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

网上的一些地方说,应该存储一个单独的数据结构,跟踪是否选择了一个单元格,但其他人说这应该自动处理。不知道该相信什么。

我非常确定这里的问题是当我执行tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark时,会为屏幕索引设置复选标记,而不是相对于整个列表的实际索引。知道如何解决这个问题吗?提前致谢。

编辑:根据要求,这里是我的cellForRowAtIndexPath功能......虽然它对选定的单元格没有做任何事情......也许应该这样做。

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

    if self.friendRecords[0].count == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: NO_FRIENDS, for: indexPath)
        return cell
    }

    let friend = self.friendRecords[indexPath.section][indexPath.row]
    let cell = tableView.dequeueReusableCell(withIdentifier: FRIEND_CELL, for: indexPath)

    if let friendCell = cell as? FriendTableViewCell {


        friendCell.username.text = (friend.object(forKey: USERNAME) as! String)

        if friend.object(forKey: REAL_NAME) == nil {
            friendCell.name.isHidden = true
        }
        else {
            friendCell.name.text = (friend.object(forKey: REAL_NAME) as! String)
        }

        if friend.object(forKey: PROFILE_PIC) == nil {
            friendCell.profilePic.image = UIImage(named: DEFAULT_PROFILE_PIC)
        }
        else {
            let img = friend.object(forKey: PROFILE_PIC) as! CKAsset
            friendCell.profilePic.image = UIImage(contentsOfFile: img.fileURL.path)
        }
    }

    return cell
}

0 个答案:

没有答案
相关问题