2011-05-10 3 views
1

Как создать UITableViewCell с текстовым полем и кнопкой. Я попытался с этим кодом, но кнопка невидима, а текстовое поле - нет.Создать 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]; 
    } 

ответ

0

doneButton.frame = CGRectMake (520, 10, 30, 30);

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

+0

Забудьте! Я кодирую ipad. Извини за это! – 9891541

1

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

[doneButton release]; 

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

+0

Ох. Чувствуешь себя глупо! Спасибо вам! – 9891541

0

Выполните следующие действия.

doneButton.frame = CGRectMake (520, 10, 30, 30);

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

Сделайте еще одну вещь в приведенном выше. Добавьте цвет фона к своей кнопке.

[doneButton setBackgroundColor: [UIColor blackColor]];

Смежные вопросы