用文本框和按钮创建UITableViewCell?

时间:2011-05-10 02:33:04

标签: 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];
    }

3 个答案:

答案 0 :(得分:1)

删除此行:

[doneButton release];

您正在使用buttonWithType创建按钮,该按钮将返回自动释放的对象。

答案 1 :(得分:0)

  

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

此代码中出现问题。 iPhone在纵向模式下有320宽度,在横向模式下有480。如果设置x = 520,则无法看到它。

答案 2 :(得分:0)

执行以下操作。

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

问题在于以上几行。如果你在iPhone应用程序中使用它,那么它的宽度只有320像素。如果你在iPad上使用它很好。

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

[doneButton setBackgroundColor:[UIColor blackColor]];

相关问题