Generic-user-small Jonathan Wad... 3 posts

Hi, I’m finding the book very helpfull and it’s clearing up many of my misunderstandings.

Please, can we have more on customising UITableViewCell? The apple documentation hasn’t really helped me understand the best ways to tailor:
  • Cell size.
  • Multiple text components in a cell.
  • Making long text (textview or textfield) scroll.
  • When the table is put into edit mode, what do I have to do to allow for the delete icons flowing in (I tend to end up with overlaid text).

It appears that many applications will probably need to tailor the way table cells are formatted.

Many thanks

 
Generic-user-small Joseph Barkley 3 posts

I second that request.

 
Generic-user-small Greg de Vitry 1 post

I found the Apple SeismicXML tutorial showed how to use a custom Cell. http://developer.apple.com/iphone/library/samplecode/SeismicXML/index.html (good for an XML download tutorial too).

In the table creation, you use TableViewCell *cell = (TableViewCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];

The ‘TableViewCell.m’ file contains the drawing of the cell.

To auto change the height of a text field, you need to determine the orientation of the phone/touch.


                // getting the cell size
                CGRect contentRect = self.contentView.bounds;
                CGFloat boundsX = contentRect.origin.x;
        CGRect frame;
        CGRect contentRect = self.contentView.bounds; /* UIView *myContentView = self.contentView; */
        NSString *text = self.serviceLabel.text; /* serviceLabel is the label that I am using */
        UIFont *font = self.serviceLabel.font;
        CGSize constraint = CGSizeMake(contentRect.size.width,contentRect.size.height);
        CGSize size = [text sizeWithFont:font constrainedToSize:constraint];
        frame = CGRectMake(boundsX + 10, 24, contentRect.size.width-20, size.height+30);

 
Littleboy_small Roman Busyghin 21 posts

Also, you can design UITableViewCell using Interface Builder, attach IBOutlet to it and return this cell from - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath.

 
Photo_13_small Bill Dudney 653 posts

Hi All,

Roman’s suggestion is great except that you need to load the nib file over and over. The TV expects a new TVCell for each new visible cell so you need to use the dequeue method to get a repeated cell.

Chris is working on getting this technique into the table view chapter. Look for it soon in an upcoming beta.

Thanks again!

 
Generic-user-small Timothy Schmitz 1 post

Really looking forward to this update to the table view chapter. I’ve experimented with loading UITableViewCells from IB, and run into a few problems. Keep up the great work, guys!

 
Generic-user-small Adam Venturella 4 posts

I agree, It took me FOREVER to discover that I needed to make a new XIB that was a UITableViewCell, and set an IBOutelt and load the resource so it bound to the IBOutlet. It would be good to have something that covers this process, as it’s really not documented anywhere. I also remember listening to Late Night Cocoa #33 and, I forget which of the 3 authors it was, mentioned something to the effect of the proper way to do custom TableViewCells. The conversation never touched on it again. In fact, I bought the beta book for that sole reason, to see if the explained what that meant or if the IBOutlet thing was the way to go.

In any case, I think it would be a good sub chapter to add to the book. =)

 
N614592285_2406_small Chris Adamson 153 posts

Like Bill said, I’m going to add this to the table chapter in an upcoming errata sweep.

But actually, I already did sort of sneak this into the book, at least its sample code, via the backdoor. Look in the database chapter at Figure 8.2. Notice that the table cells have three labels, laid out with different sizes, colors, and positions.

And nobody’s posted an HTF?

Here’s the summary of the approach I’m generally using:

  1. Create a NIB whose only content is a UITableViewCell. Lay stuff out, customize fonts and colors, etc.
  2. In Xcode, create an NSObject subclass that will represent the cell. Create IB outlets for the parts of the cell you’re going to modify from code (usually just labels, but potentially UIImageViews and other interesting widgets)
  3. In IB, use the Identity Inspector to set the class of the cell object to the class you just created. Connect the labels and other cell subviews to the outlets you just created.
  4. In Xcode, write a factory class that has a createTableCell method, which loads the NIB via loadNibNamed:owner:options:, searching the NIB’s contents for an instance of your class, returning it when found.
  5. In your table delegate, use the factory instead of allocating a new table cell when you fail to dequeue a reusable cell from the table.

I’ve now done this in a few apps of my own, and my technique is a little refined from what you see in the database example. That code, at least in the version that’s checked in, doesn’t use the identifier (wrong!), and uses a singleton for the factory instead of just using a class method. I also want to take a little more time to try to find a “deep clone” method that I could use for generating new cells within the factory instead of reloading the NIB every time. I’ll be working through these issues soon before touching the table chapter.

But if you’re keen to get unblocked, there you go…

8 posts, 8 voices