tableview没有使用notificationcentre和tableview.reloaddata()刷新

时间:2016-07-19 12:34:14

标签: ios swift uitableview uiviewcontroller

这是我使用notificationcenter和tableview.reloaddata()单击自定义tableview单元格上的删除按钮时用于刷新tableview的代码。我搜索了一堆其他代码,我认为我的代码看起来很好。我不知道为什么它不刷新。

这是我的tableview

class postCell: UITableViewCell{

@IBOutlet weak var deleteBtn: UIButton!
@IBOutlet weak var postImg: UIImageView!
@IBOutlet weak var category: UILabel!
@IBOutlet weak var location: UILabel!
@IBOutlet weak var title: UILabel!
@IBOutlet weak var price: UILabel!
var prices = String()
var notes = String()
var comments = String()
var locations = String()
var categories = String()
var school = String()
var username = String()
var date = String()


@IBAction func deleteAction(sender: AnyObject) {
    let request = NSMutableURLRequest(URL: NSURL(string: "http://www.percyteng.com/orbit/deletePost.php")!)
    request.HTTPMethod = "POST"
    let postString = "name=\(username)&location=\(locations)&price=\(prices)&notes=\(notes)&school=\(school)&category=\(categories)"
    request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)

    let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
        data, response, error in

        if error != nil {
            print("error=\(error)")
            return
        }

        print("response = \(response)")

        let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
        print("responseString = \(responseString)")
    }
    task.resume();           NSNotificationCenter.defaultCenter().postNotificationName("NotificationIdentifie‌​r", object: nil)


}

override func awakeFromNib() {
    super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated:animated)
}
}

这是我的自定义tableview单元格

{{1}}

3 个答案:

答案 0 :(得分:0)

尝试在viewDidAppear()中重新加载表。

答案 1 :(得分:0)

当你点击"删除"看起来你没有更新values属性。按钮。

func methodhandlingTheNotificationEvent(){
    // you have to update the `values` array so that it doesn't contain the value you've just removed.
    tableView.reloadData()
}

答案 2 :(得分:0)

您必须在重新加载tableview之前更新数据源,否则只会影响您的tableview,否则您将始终获得旧表。

我认为maindatacellforrowAtIndexpath中的主要数据源。因此,您必须更新或删除数据源,然后重新加载数据。