How to only make one cell selectable - iphone

In my normal tableview I got a bunch of switches and segments, and I don't want them to be selectable, so I set:
tableView.allowsSelection = FALSE;
Now one of the cells, I want to point to a new view, and thereby make it selectable. But there's no cell.allowSelection or anything, how do I solve this? How do I make it, so that it can be pushed and direct you to another view? I don't want them all to be selectable, since that's ugly...

There is not an allowsSelection property on cells, but there is a similiar property.
Set cell.selectionStyle to UITableViewCellSelectionStyleNone for cells you don't want to the user to be able to select.
(You will have to set table.allowsSelection back to YES.)
Configured this way, you'll still get -tableView:didSelectRowAtIndexPath: for any cell the user taps; just ignore it for cells that you don't intend to be selectable. (Thanks tc.)

Related

Make tableView readonly but scrollable

How can i easily make a tableview readonly but scrollable?
i tried to set tableView.isUserInteractionEnabled = false.
However, it disabled the scrolling action either.
I'm not sure I understand your question but a table view has no default interaction on it unless you write it.
You create the table view, configure the data source and the cells and thats it.
You can add behavior to it so when you tap on a cell allows you to navigate to detail, delete it, etc.
If you don't want the cells to react visually when the user taps them, just set the selectionStyle of the UITableViewCells to UITableViewCellSelectionStyleNone

image is reverting to original when scrolling uitableview

I have a button inside each cell. When it's pressed, the image is changed (basically a checkbox) to denote a selection. When you scroll to the bottom ... then scroll back up to the top. The image is reverted to the original image.
This question is pretty similar to this:
Preserve Cell Image After Scrolling UITableView
And others. But, I can't seem to find a good answer. I understand that's it's reverting back to how the uitableview is setup when the cell goes off the screen. But, how do I save the changed image to the uitableview so when it scrolls it doesn't revert to the original?
Thanks in advance! =)
It's changing back because cells are reused. When your cell is going off the screen it is taken out of the view and put back into the reuse pool. Then you're getting it out of the queue again in cellForRowAtIndexPath and setting it back up as the default.
The question you linked to is exactly what you should follow. You should store the state of your cell in your view controller and then when you set it up again in cellForRowAtIndexPath you should load that state and set up the cell appropriately.
One simple way for your method would be to have an NSArray which you set up to be the same size as the number of rows in your table and then in that just store an NSNumber for each row which contains a boolean value on or off for your selection state. When the user toggles, toggle the value in the array and then in cellForRowAtIndexPath read that value and set it up appropriately.
I'm assuming the checkbox in your table view cell is changing state to a selected state because a user selected it. You shouldn't use UI elements to maintain the state of your app. That is, when the user taps the checkbox, you should use that event to somehow reflect that state change in a data object in your app. Then, when that cell needs to be displayed again, you configure it with the state you previously saved. This allows for things like cell reuse, and view unloading and is all-around a good habit.

custom uitablviewcell does not call didSelectRowAtIndexPath in the controller

I have a custom UITableViewCell completely written in code (no IB), it has an accessory button that simply calls didSelectRowAtIndexPath on the table view, and it works correctly and the method is called without problems.
However, when I tap on the cell itself (not on the accessory view) nothing being called, why ?
EDIT: the code is huge to put here ... however, the custom cell contains a ton of labels, couple images and scroll view ...
This is a shot in the dark, but if each cell has many different objects on it (i.e. images, labels, etc) then it may not be working because those objects are what the user is hitting when they try to click a cell. Does the cell turn blue (indicate selection) at all? If not, try hiding/removing those objects for now and see if it works.
If that is the case, then what you may want to do is create an invisible cell or button that sits on top of the other objects and calls didSelectRowAtIndexPath from behind the scenes.
This should solve your problem:
Raise selection event (didSelectRowAtIndexPath) when subview of UITableViewCell is tapped
Try setting your view's userInteractionEnabled property to NO.
This will make it ignore all touch events, and then the views under it will be able to catch these events. - Felipe Sabino
I'd partially answer my question: the wide scroll view is preventing the cell from calling didSelectRowAtIndexPath, removing the scrollView will solve the problem, however, I want to call this method with the existence of the scrollView ... anyone got ideas would be highly appreciated ...
You must post your code to understand what have you done...You have to check out this example to understand whether your code is correct or not...
http://www.edumobile.org/iphone/iphone-programming-tutorials/impliment-a-custom-accessory-view-for-your-uitableview-in-iphone/

How to add UITableViewCellAccessoryCheckmark in the middle of the cell in iphone?

In my iPhone application I have used (UITableViewCellAccessoryCheckmark) to add the checkmarks in the cells, - it is added to the cell at the right side of the cell.
I want the checkmarks to display in the middle of the cell and after that a string should display. So that the user can set the cell item as checked or unchecked.
Please provide any solution or any code for adding the (UITableViewCellAccessoryCheckmark) in the middle of the cell.
There is no built in way to do this. You will have to create a cell, then in tableView:cellForRowAtIndexPath: you will need to either add a custom subview, or return a custom cell and tell it to display the custom checkmark you added based on the data state.
The accessory view of the cell is always on the right side, outside of the cell's content view (see the Table View Programming Guide for more on this). If you want to do something like this, you really need to create your own cell class, and draw the checks yourself.
Speaking as a user, this design seems sort of confusing, and definitely not what I'd expect. What's the string you're displaying to the right of the check? Maybe something like the UITableViewCellStyleValue1 style cell would work, instead? (See Standard Styles for Table-View Cells for more.)

Toggling display of image in UITableViewCell

Depending on the result of a condition, I want to display a UIImageView in a table cell. Otherwise display UITableViewCellAccessoryCheckmark. I'd like to construct the cell in IB. The part I'm not sure of what to do with the UIImageView when I don't want it displayed. If I were constructing it all programmatically, I'd add the UIImageView as needed. But since it will be done in IB, the UIImageView is always there. Should the default be leave the cell alone (image displays), otherwise remove UIImageView and display UITableViewCellAccessoryCheckmark? If that is done, will I need a tag on UIImageView so it can be accessed and removed?
You can easily control the visibility of any control, including UIImageView. If you'd like to build things in IB then one solution is to add the controls you need, expose them as properties, and then hide the ones you don't want for a given cell.
E.g.
cell.image.hidden = YES;
When hidden they have no draw-overhead, and although your cell may have thousands of rows there will be very few actual cells, so it's a fairly efficient solution. Just remember that cells are reused if you call [tableView dequeueReusableCellWithIdentifier] (which you should do) so you'll have to explicitly show/hide control whose visibility may have been changed.
An alternative is to have cells with and without images and choose the correct one when adding data to your table. For cells that are similar managing two similar-yet-minorly-different assets would probably be a pain though.