Toggling display of image in UITableViewCell - iphone

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.

Related

How can I make my custom UITableViewCells as fast as possible?

I'm creating custom UITableViewCells, and I'd like to make them in a way that allows the table view's scrolling to be as smooth as possible. In their current state, with a custom background image on each cell using the cell's backgroundView property, scrolling is is still fairly smooth.
But my question is, how can I add content to the cell and maintain this? The cells are fairly different from each other -- one may have a single label, another may have two labels and an icon, and another may have a bunch of other controls.
I've found that using unique cell identifiers for non-similar rows makes the overall experience laggy, so I need a method that allows me to use the same cell identifier and have very different cells.
Should I be using an XIB for this? If not, how should my subclass function? I was thinking of adding all of the controls to the cell, and only hiding/using the ones necessary at the time. Is there a cleaner way?

Accessibility in custom drawn UITableViewCell

UITableViewCells scrolling speed increases dramatically when you do custom drawing, however, Accessibility breaks. How should one add accessibility support to a cell like this?
Old question, but iOS has built in support for this kind of accessibility. Take a look at the UIAccessibilityContainer informal protocol which will allow you to define rects in your view and label them appropriately. Thus, if you draw a big X at 0,0 with a size of 44x44, then you can set the accessibilityLabel for that rect to "Delete".
You'll need to set the relevant accessibilityLabel yourself if you're using custom views to display information (in this case, to draw a table cell). Depending on exactly which view you've taken over drawing for, the label you need might be either the table cell's (for a UITableViewCell subclass) or your custom content view's.
If you're drawing complex information in your custom cells, consider including all that you can reasonably vocalise in the label, separated by commas as discussed in the accessibility guide. What to include is usually common sense but talking to a regular voiceover user can be really helpful, especially regarding what piece of information they want to know first.
I'm a bit in the dark as well on what you are trying to ask. To me, it sounds like you're having trouble accessing methods/objects on your custom cell. When you have a class named CustomCell, use a line like
CustomCell *cell = (CustomCell *)[localTableView dequeueReusableCellWithIdentifier:MyIdentifier];
to create a custom cell.
Suppose your custom cell contains a few labels. You can then access them easily using something like
[cell.aLabel setText:#"Accessed by load"];
When you try to access methods, for example: an IBAction when pressed on a button inside a cell, you should declare those methods in your class CustomCell (and not in your table view's class). After that, link your button's connector to the CustomCell's connector.
Note: this connector won't necessarily be in File's Owner. I'm using XCode 4, and I see 3 objects: File's Owner, First Responder, and Custom Cell. My IBAction is located in Custom Cell, even though I'm used to having it linked to File's Owner.
I hope this was of any help.

Best way to avoid memory leaks for Multiple Buttons in UITableViewCell

I've got an array of buttons in a UITableViewCell.
I populated them all through the cellForRowAtIndexPath method, but my tableview gets sluggish even though I have released everything.
Should I be using a custom UITableViewCell to populate?
Any suggestions on how to make this as smooth as possible for the user would be great.
Screenshot below.
You could create a custom cell / custom view pair for this specific cell, where you can draw all yours buttons in drawRect:. However, this would essentially draw all your buttons as an image so you wouldn't be able to tap them, but I guess you can always create a UITapGestureRecognizer to your cell (you'd still have to figure out which button was pressed by examining x,y values).
Still, I don't see any point in adding your tag buttons inside a UITableViewCell. You could come up for an alternate design in your UI, i.e. a UIView presented on top of the table, or a modal controller maybe.

How to do edit-in-place in a UITableView?

Is there a standard way to set up a table to allow editing-in-place, kind of like this:
I only need editable text at the moment, but I might need UISwitches or UISliders in the future.
Yep. Just add a UITextField, with its font and textColor set to appropriate values, as subviews of the table cell's contentView. You probably want to give the field a tag as well, so that you can easily grab a reference to it using the contentView's -viewWithTag: method.
With short forms you can get away with keeping an array of cells, one for each field, and handing them off to the table view without going through the -dequeueReusableCellWithIdentifier: mechanism, but if you've got a lot of stuff to enter then it gets more complicated. In that case, you probably want to assign a different reuse identifier to each type of cell—one for text-field cells, one for switch cells, one for slider cells, etc. Once you've dequeued or created the row's cell, you'd then grab its control from the content view (as above) and set its value from wherever you've stored that.
Edit in-place using a "Content: Static Cells" and make it look like a default styled UITableviewCell.
In UITableView create two cells: one Custom and one Subtitle Styled.
Copy the "title" label corresponding to the Subtitle styled UITableViewCell to the Custom UITableViewCell.
In Custom UITableViewCell, drop a UITextField. Copy in this the style parameters from the Detail UILabel from Subtitle Styled UITableViewCell in this UITextField.
Do the necessary arrangements.
The looks pretty equal, aren't they?
Now, do the IBoutlet stuff.

Content of custom table view cells not drawing correctly

I have several parts in my app where I use custom table view cells.
Their content is created with subviews.
The problem is that on some of these cells, the content does not appear at all or does not appear correctly until after the cell was selected for the first time.
One example is a custom cell which has a custom subview which can be set after its creation. This view does not appear at all before I selected the cell and its views were redrawn. Calling -[setNeedsDisplay] in the subview's setter method does not help either.
The problems was that I was using the cells themselves to calculate their height. For some reason, the subviews (which were part of the cell used to calculate the height) weren't appearing correctly in the cells that were used for the actual displaying.
Therefore my advice: Never use a UITableViewCell to calculate its own height. This may work in principle (it doesn't crash), but might bite you later in unexptected and hard-to-debug ways.