Создать UITableViewCell с текстовым полем и кнопкой?

#iphone #ipad

#iPhone #iPad

Вопрос:

Как я могу создать UITableViewCell с текстовым полем и кнопкой. Я пытался использовать этот код, но кнопка невидима, а текстовое поле — нет.

 cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierWithTextBox];
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierWithTextBox] autorelease];
        cell.accessoryType = UITableViewCellAccessoryNone;
        CGRect textRect = CGRectMake(10, 20, 500, 31);
        UITextField *myfield = [[UITextField alloc]initWithFrame:textRect];
        myfield.borderStyle = UITextBorderStyleRoundedRect;
        myfield.font = [UIFont systemFontOfSize:22.0];
        myfield.adjustsFontSizeToFitWidth = YES;
        myfield.minimumFontSize = 2.0;

        myfield.clearButtonMode = UITextFieldViewModeWhileEditing;
        myfield.keyboardType = UIKeyboardTypeDefault;
        myfield.autocorrectionType= UITextAutocorrectionTypeNo;
        myfield.autocapitalizationType=UITextAutocapitalizationTypeNone;
        myfield.returnKeyType=UIReturnKeyDone;


        UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        doneButton.frame = CGRectMake(520, 10, 30, 30); // position in the parent view and set the size of the button
        [doneButton setTitle:@"Registry now" forState:UIControlStateNormal];
        // add targets and actions
        [doneButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        // add to a view            

        [cell.contentView addSubview: myfield];
        [cell.contentView addSubview: doneButton];
        [doneButton release];
        [myfield release];
    }
  

Ответ №1:

Удалить эту строку:

 [doneButton release];
  

Вы создаете кнопку, используя buttonWithType которая возвращает автоматически выпущенный объект.

Ответ №2:

Нажата кнопка.frame = CGRectMake(520, 10, 30, 30);

Проблема возникла в этом коде. iPhone имеет ширину 320 в портретном режиме и 480 в альбомном режиме. Если вы установите x = 520, вы не сможете его увидеть.

Комментарии:

1. Забыть! Я пишу для ipad. Извините за это!

Ответ №3:

Сделайте следующее.

Нажата кнопка.frame = CGRectMake(520, 10, 30, 30);

Проблема в приведенной выше строке. Если вы используете это в приложении для iPhone, то его ширина составляет всего 320 пикселей. Если вы используете это на iPad, это нормально.

Сделайте еще одну вещь из приведенного выше. Пожалуйста, добавьте цвет фона к вашей кнопке.

[Нажата кнопка setBackgroundColor:[UIColor blackColor]];