UITextField в UITableViewCell - PullRequest
       7

UITextField в UITableViewCell

1 голос
/ 24 марта 2012

Я хочу разместить UItextField в UITableViewCell, но у меня есть проблема.Текстовое поле не появляется.Это мой код:

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString         *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 120.0f, 45.0f)];
        self.textField.textColor = [UIColor blackColor];

        [self.contentView addSubview:self.textField];
    }

    return self;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    TextFieldCell *cell = (TextFieldCell*)[tableView  dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[TextFieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    switch (indexPath.row) {
        case 1:
            cell.textField.placeholder = @"Username";
            break;
        case 2:
            cell.textField.placeholder = @"Password";
            break;
        case 3:
            cell.textField.placeholder = @"Server Address";
            break;
        case 4:
            cell.textField.placeholder = @"Description";
            break;            
        default:
            break;
    }

    return cell;
}

Я тестирую и вызывается метод initWithStyle. Кто-нибудь может мне помочь?

1 Ответ

2 голосов
/ 24 марта 2012

Если у вас есть .xib файл для TextFieldCell , затем перетащите текстовое представление и установите все свойства из Интерфейсного конструктора, иначе вы можете создать только * .XIB для использования в качестве TableViewCell с помощью *

В файле .h

IBOutlet UITableViewCell * CUSTOM_CELL_XIB;

В файле .m в методе cellForRowAtIndexPath

static NSString *CellIdentifier = @"Cell";


UITableViewCell *cell = (UITableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) 
{
    [[NSBundle mainBundle] loadNibNamed:@"CUSTOM_CELL_XIB" owner:self options:nil]; 
    cell = CUSTOM_CELL_XIB;
}

и непосредственно перетаскивайте textField или любой другой пользовательский интерфейс, я думаю, что это эффективный способ создания пользовательских ячеек.

Это будет отлично работать

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...