vendredi 10 mars 2017

Selecting checkmarks after reloading view or reloadData in tableView issue

I'm trying to create an application to select/deselect rows in tableView, all data is loaded from coreData, and when I select an element that allows me to update it in coreData and vice versa,

at the beginning everything is OK, I load data from coreData, display all items in tableView and check rows if needed in cellForRowAtindexPath

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    var cell:UITableViewCell? = nil

    let item = (arrayToDisplay[indexPath.section] as Array<NSManagedObject>)[indexPath.row]

    cell = tableView.dequeueReusableCell(withIdentifier: "ShoppingCell")!

    let lblName:UILabel = cell?.viewWithTag(101) as! UILabel

    let attributedString = NSMutableAttributedString(string: (item.value(forKeyPath: "name") as! String?)!)

    if( item.value(forKeyPath: "bought") as! Bool)
    {
        attributedString.addAttribute(NSStrikethroughStyleAttributeName, value: NSNumber(value: NSUnderlineStyle.styleSingle.rawValue), range: NSMakeRange(0, attributedString.length))
        attributedString.addAttribute(NSStrikethroughColorAttributeName, value: UIColor.red, range: NSMakeRange(0, attributedString.length))

        cell?.setSelected(true, animated: true) //set checkMark to active/checked status

    }


    lblName.attributedText = attributedString

    return cell!
}

before that, I've enabled editingStyle like below

func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
    return UITableViewCellEditingStyle(rawValue: 3)!
}

Everything is working correctly when I load the view for the first time, when I check or uncheck I update value in CoreData and reload my tableView, I reload data from coreData and tableView in viewDidAppear, so whenever I switch between scenes there will be a refresh, using new data

When I check an item in my tableView, I save data, update my class array, and reloadData to display new changes (including text strikethrough)

and here is the issue, whenever I reload tableView, or switch between tabs (in TabBar), and Go back, checkmarks are not checked properly, and I'm not able to check them, they appear like they were checked and unchecked in less than a second,

I've checked maybe I have some conflicts in my code but everything is correct as all update operations to coreData are done properly,

I've followed sample project from Maitrayee Ghosh writen in ObjC, you can simulate the same problem by adding [tableView reloadData] to didSelectRowAtIndexPath like below

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"user selected %@",[dataArray objectAtIndex:indexPath.row]);

[tableView reloadData];}

So, How can I refresh tableView content with checkmarks status checked properly with the ability to change status, something like Google Keep checkbox notes




Aucun commentaire:

Enregistrer un commentaire