Create UITableViewCell with textbox and button?
How can i create a UITableViewCell with textbox and button. I tried with this code but the button is invisible while the textbox is not.
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 par开发者_StackOverflow社区ent 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];
}
Remove this line:
[doneButton release];
You are creating the button using buttonWithType
which returns an autoreleased object.
doneButton.frame = CGRectMake(520, 10, 30, 30);
The problem happened in this code. IPhone has 320 width in Portrait mode and 480 in Landscape mode. If you set x=520 then you can not see it.
Do the following.
doneButton.frame = CGRectMake(520, 10, 30, 30);
Problem is with the above line. If you are using this in an iPhone app, then its has only 320px in width. If you are using this in an iPad which is fine.
Do one more thing in the above. Please add a background color to your button.
[doneButton setBackgroundColor:[UIColor blackColor]];
精彩评论