is there a way to set the allowsSelection property of UITableView for each table section separately, and not for the whole table ?
Doesn't look like it. Here's what you do: use the indexPath.section value to set the cell's .selectionStyle to UITableViewCellSelectionStyleNone and to return early from -didSelectRowAtIndexPath.
In other words, you'll configure cells in certain sectinos to not flash blue when selected, and when it IS selected, not to do anything. It'll be just as if that section had its allowsSelection property set to NO.
Try using the willSelectRowAtIndexPath method and return nil for the section you don't want to allow selection on.
Related
I am unable to find a solution for this on my own, please help.
I have a table view with -/+ buttons inside that increase or decrease a value.
In the footer cell of the table is a label for the "total sum", the added value of all the cell values. When the value inside of a cell is decreased or increased, I want the footer cell text to change accordingly.
Any pointers how I go about that? I have tried to write a global function, but then I can't access the label anymore... tried it as a class method (static), but I can't seem to get it to work.
Can I somehow hook up the action that occurs in the UITableViewCell to trigger an action in the UITableView?
Thank you!
In the UITableViewDelegate associated to your table view, you should call the reloadRowsAtIndexPaths(indexPaths: [NSIndexPath], withRowAnimation animation: UITableViewRowAnimation) method to update your the row you wish to update.
Than it your UITableViewDataSource provide the footer cell with the good new value.
I'm trying to set the tableView.separatorStyle to UITableViewCellSeparatorStyleNone for a single section (or row, I suppose), but allow it to be set to the default for every other section.
As an example (of the unintended behavior):
While the rows at the bottom (and top) of the image should display the default gray line surrounding each section, for this particular section, even though I have set the backgroundColor of the cell to [UIColor clearColor], I would also like to remove the separator from this particular section.
There don't seem to be any delegate/datasource methods that allow overriding this on a per-section basis. The documentation for the separatorStyle property mentions:
UITableView uses this property to set the separator style on the cell returned from the delegate in tableView:cellForRowAtIndexPath:.
So it sounds like it's applied after I've built my cell, and the setter is only at the UITableView level, it is not visible at the ..Cell level.
the separatorStyle property can't be used for a specific cell. It will be used for all the cells. If you want to add separators just for some cells, add a UIView inside the cell's vie that will act as a separator and set the separatorStyle to UITableViewCellSeparatorStyleNone.
In cellForRow.. you can check the cell that will be provided to the table view and decide if the separator should be visible or not.
Just set a custom separator inset of 1000 for cells that you don't want to have separators.
I have a situation where I want to set the hide the cell if cell.textlabel.text == nil.
I tried using heightForRowAtIndexPath...
But this event is fired before the cellForRowAtIndexPath event and that's why I doesn't get text of the cell.
Is there any way that I can set height of cell to zero if there cell.textlabel.text == nil in cellForRowAtIndexPath event.
You can't set values of TableView - the only way to change its appearance is to tell her to reloadData, and then let delegate feed it with new, fresh ones. You can also use deleteRowsAtIndexPaths:withRowAnimation: to remove a cell, insertRowsAtIndexPaths:withRowAnimation: to insert it back later on.
From what you write I guess you structured your program in a very bad way. You should have a separation between Model, View and Controller.
I'd like to add section headers to my grouped table view's sections but I'd like them to appear seamless (see image). The default, as we're all well aware of, is rounded top corners on the first row of a grouped table view cell so it ends up looking like crap when you merge them.
Any way to specify when indexPath.row = 0 that the UITableViewCell should use row style "middle" or something like that?
If not then what are my options? I guess I could scratch the section header and use Row 0 as a quasi-header then push my array data +1 to fill the rest of the table? I'd rather not roll my own from scratch...if possible.
Sample Table http://img35.imageshack.us/img35/8181/sampletable.png
Edit:
"Crap" looks like this:
alt text http://img25.imageshack.us/img25/9748/crapsection.png
Don't do what you're doing, it's against HIG
Ok, ok, I'll tell you how to do it:
You're going to want to do your own cell background views. The default grouped one is not what you want.
When a tableview asks you for a cell, set its backgroundView and selectedBackgroundView to something that looks appropriate for its place in the tableview.
Usually, this means a UIImageView with the appropriate image, though you can go wild here with a custom view, but there are gotchas.
So in your case, you would do
if (indexPath.row > sectionRowCount - 1) {
//Not the last row
//Put in the middle background
} else {
//Put in the end background
}
Then you'll want a custom table section header, but that's pretty easy.
In your case, you probably won't have to worry about when there's just one row, so that makes things even easier.
Take a look at the tutorial here:
cocoa with love basically what you need is 3 different images. One for the top row, one for the bottom, and a 3rd for the middle rows.
You could also not use the section header, but instead use a custom cell as the first cell of the section. So when ([indexPath row] == 0), return a custom cell that is the "header" and then return the "regular" cells (offset by one row) for the rest. You'll also have to make adjustments to the numberOfRowsInSection function to return +1.
I am happily able to set the styling of my tableviewcells using the cellForRowAtIndexPath delegate, but I want to set the background of all the nonpopulated cells which are still on the screen
hey, basically any cell which is part of your tableview belongs to a particular index and particular row
so incase you are not populating a particular section via data source - you can still get a reference to a cell in that indexPath by manually calling the cellForRowAtIndexPath delegate, and passing the section as well as row index
it returns you a cell. assign it to your private variable and edit it the way you want to.
Use the visibleCells method on your table view. Then do what you wish to the cells.
Alternately, you could call indexPathsForVisibleRows to get just the index paths. Then call cellForRowAtIndexPath: to get the cell corresponding to each index path.