简体   繁体   中英

Changing UITableViewCell Text Color with Property Value of True

I have an entity called Item with an attribute called completed . I am displaying all Item objects in a custom UITableView . Then I have a function to set the Cell object property completed to true then set the text colour to grey.

This works fine when until I leave the view controller. How can I make all objects with the value of true for the property completed have a cell label text colour of grey when loading the table view controller ?

This is my action for changing the value and making the cell grey on action:

 cellCompletedAction = {(tableView: customTableView, cell: customTableViewCell) -> Void in


 let delegate:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
 let context: NSManagedObjectContext = delegate.managedObjectContext!

 let indexPath = tableView.indexPathForCell(cell)
 let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! customTableViewCell!;

 currentCell.textLabel?.textColor = UIColor.lightGrayColor

       self.itemsArray()[indexPath!.row].completed = true

        var error: NSError? = nil
        if !context.save(&error) { abort() }

 tableView.bounce(cell, duration: 0.3, bounce: 0.3, completion: nil)

    }

Loading data to table view:

 cell.textLabel?.text = itemsArray[indexPath.row].title

Can't you just do a check in cellForRowAtIndexPath ? Something like this:

let cell = ... // dequeuing your cell 
let item = ... // getting the object for this cell
if item.completed == true {
    cell.textLabel?.textColor = UIColor.lightGrayColor
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM