Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在动态表视图中删除行_Ios_Swift_Uitableview - Fatal编程技术网

Ios 如何在动态表视图中删除行

Ios 如何在动态表视图中删除行,ios,swift,uitableview,Ios,Swift,Uitableview,这是我的问题。我有一个用户数组,我正在检查地址簿中是否有用户电话号码,我想在tableView中显示用户 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return users.count } override func tableView(_ tableView: UITableView, cellForRowAt indexPat

这是我的问题。我有一个用户数组,我正在检查地址簿中是否有用户电话号码,我想在tableView中显示用户

 override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return users.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! UserCell

    let user = users[indexPath.row]
    let contact = searchForContactUsingPhoneNumber(phoneNumber: user.phoneNumber!)
    if contact == true {
        cell.textLabel?.text = user.name
        cell.detailTextLabel?.text = user.phoneNumber
        cell.backgroundColor = UIColor(r: 35, g: 35, b: 35)
        cell.textLabel?.textColor = UIColor.white
        cell.detailTextLabel?.textColor = UIColor.white

        if let profileImageUrl = user.profileImageUrl {
        cell.profileImageView.loadImageUsingCacheWithUrlString(profileImageUrl)

        } else {
            tableView.deleteRows(at: [indexPath], with: .automatic)
            tableView.reloadData()
        }
    }
    return cell
}

这是我的。如果电话号码不在通讯簿中,则会删除用户行,但会留下白色行。

您不应在cellForRowAt中修改表中的单元格数,或重新加载表视图。最好先过滤用户数组,然后使用过滤后的数组作为数据源

有点像

let usersWithPhoneNumbers = users.filter { $0.profileImageUrl != nil && searchForContactUsingPhoneNumber(phoneNumber: $0.phoneNumber!) }

然后在numberOfRows和cellForRow方法中使用usersWithPhoneNumbers数组。

您不应该在cellForRowAt中修改表中的单元格数,或重新加载表视图。最好先过滤用户数组,然后使用过滤后的数组作为数据源

有点像

let usersWithPhoneNumbers = users.filter { $0.profileImageUrl != nil && searchForContactUsingPhoneNumber(phoneNumber: $0.phoneNumber!) }

然后在numberOfRows和cellForRow方法中使用usersWithPhoneNumbers数组。

同意,删除本来不应该显示的行并不是筛选列表的好方法。事先这样做,使用过滤列表作为1:1的数据源。同意,删除本来不应该显示的行并不是过滤列表的好方法。事先这样做,使用过滤列表作为1:1数据源。