2011-05-10 79 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 2011-05-10 02:44:28

1

删除此行:

[doneButton release]; 

你正在创建使用buttonWithType它返回一个自动释放的对象按钮。

+0

哦。感到愚蠢!谢谢! – 9891541 2011-05-10 03:12:32

0

请执行以下操作。

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

问题在于上述行。如果你在iPhone应用程序中使用它,那么它的宽度只有320px。如果你在iPad上使用这个很好。

在上面做了一件事。请为您的按钮添加背景颜色。

[doneButton setBackgroundColor:[UIColor blackColor]];