how to create custom tableViewCell from xib

前端 未结 3 693
一生所求
一生所求 2020-12-10 06:31

I want to create a custom TableViewCell on which I want to have UITextField with editing possibility. So I created new class with xib. Add TableViewCell element. Drag on it

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-10 06:56

    You need to load your xib and retrieve your custom cell:

    NSArray *uiObjects = [[NSBundle mainBundle] loadNibNamed:@"yourNib" 
                                                       owner:self 
                                                     options:nil];
    for (id uiObject in uiObjects) {
         if ([uiObject isKindOfClass:[EditCell class]]) {
              cell = (EditCell *) uiObject;
         }
    }
    

    Make also sure you actually changed the tableViewCell class in your xib to EditCell. You also need to change the tableView row heigh to the right size.

    One other way is to just build your cell programmatically in your EditCell class, which I believe let's you be much more free and precise than within InterfaceBuilder:

    In EditCell.m:

    - (id)initWithStyle:(UITableViewCellStyle)style 
        reuseIdentifier:(NSString *)reuseIdentifier {
    
        if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
            CGRect textFieldRect = CGRectMake(5, 5, 300, 30);
            UITextField *textField = [[UITextField alloc] initWithFrame:textFieldRect];
            textField.tag = kTextFieldTag;
            [self.contentView addSubview:textField];
            [textField release];
        }
        return self;
    }
    

    Then in your tableViewController you create the cell the way you did and retrieve your textField with the tag.

提交回复
热议问题