Original URL: http://blog.it985.com/9683.html
When using TableView, if the cell layout is too complex, it is not intuitive to build through the code. And to constantly adjust the position, font or something. At this time, we can be more intuitive through the xib on the Tableviewcell, effectively improve the development efficiency.
First, after we have created the project, we create a new xib cell. Command+n, choose Cocoa Touch Class
Then select the UITableViewCell type and hook up also Create xib File
After that, we build the style we need on the xib of the corresponding cell.
Then match the corresponding code in the TableView
| 123456789101112 |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *cellIndentifier = @"MyTableViewCell";//这里的cellID就是cell的xib对应的名称 MyTableViewCell *cell = (MyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIndentifier]; if(nil == cell) { NSArray *nib = [[NSBundle mainBundle] loadNibNamed:cellIndentifier owner:self options:nil]; cell = [nib objectAtIndex:0]; } _tableView.rowHeight = cell.frame.size.height;//注意,这里我们要把table的rowHeight设为和cell的高度一样 return cell;} |
And then we'll see how it works.
Finally, the demo
Custom UITableViewCell via Xib
"Go" IOS via Xib custom UITableViewCell "original"