Swift如何在editActionsForRowAtIndexPath调用后重新加载tableview

时间:2016-05-15 02:37:33

标签: ios swift uitableview

我想在editactionsForRowAtIndexPath函数中调用一个动作后更新我的tableview单元格。

我尝试从动作处理程序,viewwillappear函数和其他几个区域调用self.tableView.reloadData,但它在调用操作后从不重新加载。

用户向左滑动然后点击确认按钮确认约会并更新数据库中的值。

对象是让函数完成更新值,然后通过重新加载来更新表,以便在cellforRowAtIndex中可以循环使用,并根据数据库中的值为每个单元格着色不同的颜色。

但似乎没有任何效果。

override func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [UITableViewRowAction]  {

    //confirm appointment
    let confirm_action = UITableViewRowAction(style: UITableViewRowActionStyle.Default, title: "Confirm", handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
        self.index_number = indexPath.row

        let confirm_menu = UIAlertController(title: nil, message: "Which request?", preferredStyle: .ActionSheet)

        let number_1 = UIAlertAction(title: "Choice 1", style: UIAlertActionStyle.Default, handler: self.confirm1)

        let number_2 = UIAlertAction(title: "Choice 2", style: UIAlertActionStyle.Default, handler: self.confirm2)

        let number_3 = UIAlertAction(title: "Choice 3", style: UIAlertActionStyle.Default, handler: self.confirm3)

        let cancel = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil)

        confirm_menu.addAction(number_1)
        confirm_menu.addAction(number_2)
        confirm_menu.addAction(number_3)
        confirm_menu.addAction(cancel)

        self.presentViewController(confirm_menu, animated: true, completion: nil)
   self.tableView.reloadData()
        })

 return [confirm_action]
 }


//confirm choice1
func confirm1(alertAction: UIAlertAction!) -> Void {
    tableView.sectionIndexColor = UIColor.greenColor()


    //Update database entry to confirmed
    let query = PFQuery(className: "Schedule")

    query.getObjectInBackgroundWithId(object_ids[index_number]){
        (Schedule: PFObject?, error: NSError?) -> Void in

        //successfully loaded schedule request
        if(error == nil && Schedule != nil) {
            Schedule!["confirmed"] = "yes1"
            dispatch_async(dispatch_get_main_queue()){
                Schedule!.saveInBackground()
            }
        }
    }

    //update table
    self.tableView.reloadData()

    index_number = 0
}


 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("schedules", forIndexPath: indexPath)


  //load cell text depending on confirmed or not
    if(confirmed_status[indexPath.row] == "yes1"){
        cell.textLabel?.text = "\(users_names[indexPath.row]) \nPAID: \(paid_status[indexPath.row]) \nConfirmed for: \(location1) on \(date1)"
        cell.textLabel!.textColor = UIColor.greenColor()
        cell.editingStyle
    }
 else{

        cell.textLabel?.text = "\(users_names[indexPath.row]) \nPAID: \(paid_status[indexPath.row]) \n#1 \(location1) on \(date1) \n#2 \(location2) on \(date2) \n#3 \(location3) on \(date3)"
    }


    //To enable word wrap for long titles
    cell.textLabel!.numberOfLines = 0


    return cell
}

0 个答案:

没有答案