Sunday, August 21, 2011

Making Smarter Table View Cells

/ Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}

cell.textLabel.text = [[data objectAtIndex:indexPath.row] objectForKey:@"title"];
cell.textLabel.font = [UIFont boldSystemFontOfSize:18];
cell.textLabel.numberOfLines = ceilf([[[data objectAtIndex:indexPath.row] objectForKey:@"description"] sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height/20.0);
cell.detailTextLabel.text = [[data objectAtIndex:indexPath.row] objectForKey:@"description"];
cell.detailTextLabel.font = [UIFont systemFontOfSize:14];
cell.detailTextLabel.numberOfLines = ceilf([[[data objectAtIndex:indexPath.row] objectForKey:@"description"] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap].height/20.0);

return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

NSString *titleString = [[data objectAtIndex:indexPath.row] objectForKey:@"title"];
NSString *detailString = [[data objectAtIndex:indexPath.row] objectForKey:@"description"];
CGSize titleSize = [titleString sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];
CGSize detailSize = [detailString sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300, MAXFLOAT) lineBreakMode:UILineBreakModeWordWrap];

return detailSize.height+titleSize.height;
}
For related posts
Click Here!

Wednesday, July 13, 2011

The first thing that you need to do before you add a UITableView to your application is to create a data source for your model so that you can feed the data from your model to the table view. To do this, you need to understand the roles of two key protocols: the UITableViewDataSource protocol and the UITableViewDelegate protocol. They are used to provide data and customization, respectively, to your UITableView instances.


The UITableViewDataSource is used to provide data from your model class to the UITableView. It has methods that you are expected to implement that are called by the UITableView when it needs to load its data. These methods allow you to feed your data to the UITableView without coupling the UITableView directly to your model.


he UITableViewDelegate protocol is used to provide customization for how the UITableView behaves. For example, there are methods on the UITableViewDelegate protocol that allow you to customize whether or not particular rows can be edited. In this way, imagine that the UITableView, whenever it needs to know how you want it to behave, asks the UITableViewDelegate what to do.