I'm trying to add check marks to my UItableview cells. I want to checkmarks to display like circle buttons on the left margin like this image:
I am unable to get the checkmarks in the left margin with my code and instead I keep getting checkmarks on the right that display only when tapped. Here is my current code:
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
cell.accessoryType = .None
}
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let cell = tableView.cellForRowAtIndexPath(indexPath) {
cell.accessoryType = .Checkmark
}
}
Any idea on how i can get the checkmarks to display on the left like the image?
I've made for you whole sample application for this.
just try this.
And this is what it looks like.
Related
I have a dark gray view background with a transparent tableview. I'm using the following code to try and stop cell highlight when a cell is clicked. It works except right when the cell is initially clicked, I see a highlight. I then transition to another scene after that. When I come back, the cell is not highlighted.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var selectedCell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
selectedCell.contentView.backgroundColor = UIColor.clearColor()
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}
How do I disable the initial cell highlighting that is still going on?
Set UITableViewCell selection style none
cell.selectionStyle = .None
I found that the other answers did not work for me as they required a double click to select. This did work.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
UITableViewDelegate has methods to deal with cell highlights, probably
tableView(_:shouldHighlightRowAt:) is what you are looking for
Check the documentation for the other methods
The simplest way to prevent highlighting is setting selectionStyle to None. Here's how you can achieve this :
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.selectionStyle = .None
return cell
}
Now why would repeated cells show up in a UITableView ?
As you can see in this GIF, I press on a cell button to do some fade effect, but other cell gets affected too !
https://media.giphy.com/media/xT0BKL5KnCgEjaXm9i/giphy.gif
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return numberOfCells
}
and the count of cells is always 10
numberOfCells = 10
I always make tableviews and I'm sure of the setup, is this a bug by apple's side?
EDIT :
How the cells get created:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("brandCell", forIndexPath:indexPath) as! CustomCell1INF
cell.selectionStyle = UITableViewCellSelectionStyle.None
cell.frame = self.view.frame
return cell
}
This happens because the UITableViewCell are being reused.
You changed the cell when you press the button, you need to keep track of that in your data source model.
In cellForRowAtIndexPath you have to add a condition to check if that button was pressed or not, then you display the appropriate view accordingly.
This is what my story board shows
However, the right arrow is not there when I run on simulator
I don't know why and I don't know what code I should show you because I actually didn't make that arrow myself, I just selected the cell's accessory as Discolosure Indicator
update 1
there is no overlap, this is from the debugger:
The problem is that you implemented layoutSubviews, but never called super.layoutSubviews(). Thus the disclosure indicator was never laid out.
Try adding this inside your tableview method
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
//Complete Code
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell")
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
return cell
}
try adding disclosure indicator programmatically in cellForRowAtIndexPath:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
...
cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
...
}
Try using preview in the assistant editor. Most likely one of the images is overlapping the indicator and you need to adjust your constraints.
I have a grouped UITableView in which I display some car data in the second section. I load the image from a web server using SDWebImage. In its callback I resize the picture and update my image view.
However, as soon as I update my image view, the UITableView separator is cut off.
For illustration purposes I've given the respective elements background colors
When the image is not loaded yet, it looks like this:
When the image is updated, it looks like this
Notice that the row separator is somehow cut off between the UITableView cells even though no subview (the UIImageView) is hiding it.
Depending on the UITableView section the row height varies, so I've overwritten the heightForRowAtIndexPath UITableView delegate
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.section == 0 {
return GFFloat(44)
}
else {
return CGFloat(220)
}
}
Can someone tell me why the separators disappear and how I can fix this? I've read about reloading the next UITableViewCell when I update the image, but since I display a lot of cars, my app crashes when I try this.
Just add below method it will resolve separator issue.
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if cell.respondsToSelector("setSeparatorInset:") {
cell.separatorInset = UIEdgeInsetsZero
}
if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") {
cell.preservesSuperviewLayoutMargins = false
}
if cell.respondsToSelector("setLayoutMargins:") {
cell.layoutMargins = UIEdgeInsetsZero
}
}
Solution for Swift 5
Add this to your controller:
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)) {
cell.separatorInset = UIEdgeInsets.zero
}
if cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins)) {
cell.preservesSuperviewLayoutMargins = false
}
if cell.responds(to: #selector(setter: UIView.layoutMargins)) {
cell.layoutMargins = UIEdgeInsets.zero
}
}
I have the following code
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
cell.textLabel?.text = users[indexPath.row]
return cell
}
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath) {
var cell:UITableViewCell = tableView.cellForRowAtIndexPath(indexPath)!
cell.accessoryType = UITableViewCellAccessoryType.Checkmark
}
The purpose was to show a checkmark when a row is tapped. Have two rows and I am observing the following behavior.
Tap row 0, can't see the checkmark
Tap row 1, can see the checkmark for the row 0
Tap row 0, can now see the checkmark for row 1
The selection attribute settings are (Xcode default)
Selection: Single Selection
Editing: No selection during editing
Is there anything I should do to show the checkmark when the row is tapped in addition to the above code. Looked into related questions but couldn't nail it.
This is a very common problem, I think almost everybody who does iOS programming has faced the same.
You need to change
override func tableView(tableView: UITableView, didDeselectRowAtIndexPath indexPath: NSIndexPath)
to
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
The issue is with the XCode suggestions as one types the UITableView delegate methods. Since deselect comes first alphabetically, it is a very common mistake. I used to face this issue with Objective C, it's funny to see this problem arising with Swift as well.