Tableview Accessories Don't Load Correctly

Tableview accessories don't load correctly

Cells get reused. Whenever you conditionally set a property of a cell, you need to reset that property in other cases.

The simplest solution is to set the accessoryType to .none before the loop (and before the if).

I also suggest optimizing the title a bit. You call objArray[indexPath.section].sectionObj?[indexPath.row].title many times in this code. Do it once.

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellClass") as! CellClass

let title = objArray[indexPath.section].sectionObj?[indexPath.row].title ?? "no title"
cell.titleLbl.text = title
cell.descLbl.text = objArray[indexPath.section].sectionObj?[indexPath.row].authors ?? "no authors"

cell.accessoryType = .none

for favorite in self.favourite {
if title == favourite.title {
cell.accessoryType = .checkmark
break // no need to keep looking
}
}

return cell
}

I've shown lots of other code cleanup as well.

Cell accessorytype doesn't reload on [tableview reloadData]

If the cell is not nil (if it has been dequeued from the table view) then you don't actually, as it is, change the accessory type. Drop the else, ie change

else if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

to

if ([tableViewListChecked containsObject:[NSString stringWithString:cell.textLabel.text]]) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}

Can a standard accessory view be in a different position within a UITableViewCell?

No, you cannot move where the accessory view is. As an alternative you can add a subview like the following;

[cell.contentView addSubview:aView];

Also, by setting the accessoryView property equal to something, the accessoryType value is ignored.

UITableViewCell Accessory is too wide on iPad

This is due to Apples new readable content margins in iOS9 intended to make content more readable in wider views (iPad Pro).

You can turn this feature off by setting

tableView.cellLayoutMarginsFollowReadableWidth = false

in viewDidLoad

Custom Accessory Button in UITableViewCell

I can think of two possible reasons:

  • The problem might be that the button has no size, so it's invisible. Add this line, after setting the image:

    button.sizeToFit()
  • Another possibility is that you have no image named "arrow-right-light.png". That wouldn't crash your existing code, but it would prevent the button from having any image so you wouldn't see it. Try saying UIImage(named: "arrow-right-light.png")! instead, just as a test; if you crash, that was indeed the problem, and then you can figure out what the correct name is.



Related Topics



Leave a reply



Submit