UITableView单元格问题

时间:2016-04-11 07:15:16

标签: ios iphone xcode swift uitableview

我在桌面视图中使用自定义单元格。我的手机里有一个开关。当我切换某些单元格的开关时,我的开关从随机单元格中消失。只有当我向交换机提供目标操作时才会发生这种情况。当我评论addTarget代码时,表视图工作正常。有人可以帮忙吗?

自定义单元格的快照为:enter image description here

我的CellForRowAtIndexPath的代码是:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = placesTableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! ApproveCustomCell
    cell.placeType.text = places[indexPath.row].type
    cell.placeName.text = places[indexPath.row].name
    cell.placePhone.text = places[indexPath.row].phone
    cell.placeWebsite.text = places[indexPath.row].website
    cell.placeAddress.text = places[indexPath.row].address

    cell.switchOutlet.tag = indexPath.row
    cell.switchOutlet.addTarget(self, action: #selector(self.approveRejectPlace(_:)), forControlEvents: .TouchUpInside)

    if places[indexPath.row].status! == "Approved" {
        cell.switchOutlet.on = true
    }else {
        cell.switchOutlet.on = false
    }

    return cell

}

更新

这是我的批准/拒绝方法:

func approveRejectPlace(sender: UISwitch!){

    var url = "\(baseUrl)\(approveRejectUrl)"
    if sender.on == true {
            url += ("Approved/\(places[sender.tag].id))")
    }else {
        url += ("Rejected/\(places[sender.tag].id)")
    }

print(self.places[sender.tag].name)

    let parameters = ["key" : encryptedKey , "salt" : encryptedSalt]
    self.view.lock()
    dataProvider.hitWebservice(url, parameters: parameters) { (jsonArray) in

        self.view.unlock()

        if let results = jsonArray[0] as? NSDictionary {

            if let result = results.objectForKey("result") as? Bool {

                if result {

                    if sender.on {
                        self.places[sender.tag].status = "Approved"
                    }else {
                        self.places[sender.tag].status = "Rejected"
                    }

//                        self.placesTableView.reloadData()

                }else {

                    invokeAlertMethod("Error", msgBody: "Failed to approve the place", delegate: self)
                    sender.on = false
                }
            }
        }

    }
}

0 个答案:

没有答案