I’d like to create a custom UITableViewCell subclass that loads content asynchronously using a URL connection. I’ve got a UITableViewCell subclass that handles all of this and a Nib file that defines the layout of the cell, but I’m having trouble linking the two. Here’s the code I’m using in tableView:cellForRowAtIndexPath:
static NSString *FavCellIdentifier = @"FavCellIdentifier";
FavouriteCell *cell = [tableView dequeueReusableCellWithIdentifier:FavCellIdentifier];
if (cell == nil)
{
cell = [[[FavouriteCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:FavCellIdentifier] autorelease];
}
cell.requestURL = [NSURL URLWithString:[NSString stringWithFormat:@"%@?%@=%i", URL_GET_POST_STATUS,
URL_PARAM_SERIAL,
[[self.favourites objectAtIndex:indexPath.row] intValue]]];
return cell;
This gives a request URL to the UITableViewCell subclass, which handles loading in the setRequestURL method.
In the FavouriteCell class I’ve kept the initWithStyle:reuseIdentifier: method as is, and in the Nib I’ve set FavCellIdentifier as the identifier, and FavouriteCell as the class. Now how do I make the FavouriteCell class load the Nib?
In order to use the nib/xib file, you’re going to have to instantiate the
FavouriteCelldifferently.Try this:
UITableViewCellto be a subclass ofFavouriteCellinstead of the defaultUITableViewCellin your xib. Do this by:FavouriteCell.File's Ownerproperty to be theUIViewControllerwhere you want to display your customUITableViewCell(pretty much same process as step #1).IBOutletproperty of typeFavouriteCellto yourUIViewController. Name it whatever you like (I’m going to call itcell).UITableViewCell, connect the IBOutlet for thecellproperty in File’s Owner to your customUITableViewCell.