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 sayingUIImage(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
Differences Between Ways of Initializing a Dictionary
Exc Bad Access After Coding Signing
Compare Value from Two Struct in Swift
Swift 3 - How to Insert Tableviewcells from Another View
Why Function Return Nil Firebase Swift
How to Change Values Inside Array Without a Loop Swift
How to Ensure Make Sure I'm Not Accessing Data Until It's Loaded In
How to Find a Maximum Value in a Swift Dictionary
Auth.Auth().Currentuser.Reload() Doesn't Refresh Currentuser.Isemailverified
How to Get Exactly the Same Point on Different Screen Sizes
Bulk Fix Hundreds of "#Selector Not Exposed to Objective-C" Errors in Xcode 9 or 9.1
Alamofireimage How to Clean Memory and Cache
Checkboxes in Uitableview State Persistence
How to Conditionally Define a Podspec Property Depending on Static or Dynamic Usage
.Sink Is Not Returning the Promise Values from a Future Publisher