XIB自定义uitableviewCell管理多个uitableviewCell控件

xib自定义uitableviewCell很方便,而且xcode6的sizeClass同样适用XIB适配就轻松不少;

但是,如果一个tableview有多个自定义的cell样式,难道一个cell样式建一个XIB文件?

  1. 绘制cell样式;
  2. 为每个样式添加标识符和申明所属类;
  3. XIB自定义uitableviewCell管理多个uitableviewCell控件
  4. 尤其注意左边的标识符顺序,因为这表示的是bundle返回的资源数组的下标,拖动无法修改顺序;
  5. 添加Cell初始化方法
  6. +(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;

    }

  7. 红色部分就是bundle返回的资源数组的下标使用;
  8. 然后就是cell创建的代理方法,因为知道什么位置放什么样式所以数组就写死了,如果不定的话需要在为空的条件下创建默认的一种样式然后再修改样式否则的话会因为cell返回为空carsh掉;
  9. -(UITableViewCell*)tableView:(UITableView *)tableView
    cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    tableView.tableFooterView=[[UIView alloc] initWithFrame:CGRectZero];

    static NSString* nsStringIdentifier0=@"identifier0";
    static NSString* nsStringIdentifier1=@"identifier1";
    static NSString* nsStringIdentifier2=@"identifier2";
    static NSString* nsStringIdentifier3=@"identifier3";


    UITableViewCellAddTravelRequest* uiTableViewCellAddTravelRequest;

    NSArray* nsArrayidentifier0=@[[NSIndexPath indexPathForRow:0 inSection:0],
    [NSIndexPath indexPathForRow:1 inSection:0]];

    NSArray* nsArrayidentifier1=@[[NSIndexPath indexPathForRow:2 inSection:0],
    [NSIndexPath indexPathForRow:3 inSection:0]];

    NSArray* nsArrayidentifier2=@[[NSIndexPath indexPathForRow:4 inSection:0]];

    NSArray* nsArrayidentifier3=@[[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;
    }

  10. 最终效果如下,这样如果再有新的样式也很好处理了;
  11. XIB自定义uitableviewCell管理多个uitableviewCell控件

你可能感兴趣的:(UITableViewCell)