xib自定义uitableviewCell很方便,而且xcode6的sizeClass同样适用XIB适配就轻松不少;
但是,如果一个tableview有多个自定义的cell样式,难道一个cell样式建一个XIB文件?
- 绘制cell样式;
- 为每个样式添加标识符和申明所属类;
- ;
- 尤其注意左边的标识符顺序,因为这表示的是bundle返回的资源数组的下标,拖动无法修改顺序;
- 添加Cell初始化方法
- +(UITableViewCellAddTravelRequest*)InitUITableViewCellAddTravelRequestAndIdentifier:(NSString*)nsStringIdentifier
{
if ([nsStringIdentifier isEqualToString:@"identifier0"])
{
return [[[NSBundle mainBundle] loadNibNamed:@"UITableViewCellAddTravelRequest"
owner:self
options:nil] objectAtIndex:0];
}
else if ([nsStringIdentifier isEqualToString:@"identifier1"])
{
return [[[NSBundle mainBundle] loadNibNamed:@"UITableViewCellAddTravelRequest"
owner:self
options:nil] objectAtIndex:1];}
else if ([nsStringIdentifier isEqualToString:@"identifier2"])
{
return [[[NSBundle mainBundle] loadNibNamed:@"UITableViewCellAddTravelRequest"
owner:self
options:nil] objectAtIndex:2];}
else if ([nsStringIdentifier isEqualToString:@"identifier3"])
{
return [[[NSBundle mainBundle] loadNibNamed:@"UITableViewCellAddTravelRequest"
owner:self
options:nil] objectAtIndex:3];}
return 0;
}
- 红色部分就是bundle返回的资源数组的下标使用;
- 然后就是cell创建的代理方法,因为知道什么位置放什么样式所以数组就写死了,如果不定的话需要在为空的条件下创建默认的一种样式然后再修改样式否则的话会因为cell返回为空carsh掉;
- -(UITableViewCell*)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
tableView.tableFooterView=[[UIView alloc] initWithFrame:CGRectZero];static NSString* nsStringIdent[email protected]"identifier0";
static NSString* [email protected]"identifier1";
static NSString* [email protected]"identifier2";
static NSString* [email protected]"identifier3";UITableViewCellAddTravelRequest* uiTableViewCellAddTravelRequest;
NSArray* [email protected][[NSIndexPath indexPathForRow:0 inSection:0],
[NSIndexPath indexPathForRow:1 inSection:0]];NSArray* [email protected][[NSIndexPath indexPathForRow:2 inSection:0],
[NSIndexPath indexPathForRow:3 inSection:0]];NSArray* [email protected][[NSIndexPath indexPathForRow:4 inSection:0]];
NSArray* [email protected][[NSIndexPath indexPathForRow:0 inSection:1]];
//先从重用队列查找 没有则创建
for (NSIndexPath* nsindexPath in nsArrayidentifier0)
{
if ([indexPath isEqual:nsindexPath])
{
uiTableViewCellAddTravelRequest= [tableView dequeueReusableCellWithIdentifier:nsStringIdentifier0];
}
}
for (NSIndexPath* nsindexPath in nsArrayidentifier1)
{
if ([indexPath isEqual:nsindexPath])
{
uiTableViewCellAddTravelRequest= [tableView dequeueReusableCellWithIdentifier:nsStringIdentifier1];
}
}
for (NSIndexPath* nsindexPath in nsArrayidentifier2)
{
if ([indexPath isEqual:nsindexPath])
{
uiTableViewCellAddTravelRequest= [tableView dequeueReusableCellWithIdentifier:nsStringIdentifier2];
}
}
for (NSIndexPath* nsindexPath in nsArrayidentifier3)
{
if ([indexPath isEqual:nsindexPath])
{
uiTableViewCellAddTravelRequest= [tableView dequeueReusableCellWithIdentifier:nsStringIdentifier3];
}
}if (uiTableViewCellAddTravelRequest==nil)
{for (NSIndexPath* nsindexPath in nsArrayidentifier0)
{
if ([indexPath isEqual:nsindexPath])
{
uiTableViewCellAddTravelRequest=[UITableViewCellAddTravelRequest InitUITableViewCellAddTravelRequestAndIdentifier:@"identifier1"];
uiTableViewCellAddTravelRequest.uiLabelLocation.text=(nsArraySectionRow[indexPath.section])[indexPath.row];
}
}
for (NSIndexPath* nsindexPath in nsArrayidentifier1)
{
if ([indexPath isEqual:nsindexPath])
{
uiTableViewCellAddTravelRequest=[UITableViewCellAddTravelRequest InitUITableViewCellAddTravelRequestAndIdentifier:@"identifier0"];
uiTableViewCellAddTravelRequest.uiLabelDate.text=(nsArraySectionRow[indexPath.section])[indexPath.row];}
}
for (NSIndexPath* nsindexPath in nsArrayidentifier2)
{
if ([indexPath isEqual:nsindexPath])
{
uiTableViewCellAddTravelRequest=[UITableViewCellAddTravelRequest InitUITableViewCellAddTravelRequestAndIdentifier:@"identifier2"];}
}
for (NSIndexPath* nsindexPath in nsArrayidentifier3)
{
if ([indexPath isEqual:nsindexPath])
{
uiTableViewCellAddTravelRequest=[UITableViewCellAddTravelRequest InitUITableViewCellAddTravelRequestAndIdentifier:@"identifier3"];}
}}
return uiTableViewCellAddTravelRequest;
} - 最终效果如下,这样如果再有新的样式也很好处理了;