2011-01-24 88 views
1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

    } 

    // Configure the cell... 
if(indexPath.row < 8) 
{ 
    CGRect textRect = CGRectMake(10, 10, 300, 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; 

    [self. addSubview:myfield]; 
} 


    return cell; 

} 

我写的文本框显示在上面的代码中的UITableViewCell(最多八个单元),但文本框在第一个单元格只显示是有什么错在上面的代码?文本框不添加到所有UITableViewCells

+2

这是第三个问题从你的头衔*“iphone编程”*。请使用描述性标题,如*“文本不在UITableView文本框中显示”*。 – DarkDust 2011-01-24 13:54:52

+2

@DarkDust:实际上,他的第五个 – BoltClock 2011-01-24 14:59:53

回答

3

的问题是这一行:

[self.addSubview:myfield]; 

更改为:

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

不过,我同意Björn Marschollek有关的设计,所以这里这个方法应该是什么样子:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifierWithTextBox = @"CellWithTextBox"; 
static NSString *CellIdentifierOther = @"CellOther"; 

UITableViewCell *cell; 
if(indexPath.row < 8) 
{ 
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierWithTextBox]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierWithTextBox] autorelease]; 
     CGRect textRect = CGRectMake(10, 10, 300, 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; 

    [cell.contentView addSubview: myfield]; 
    [myfield release]; 
    } 
} 
else 
{ 
    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierOther]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifierOther] autorelease]; 
    } 
} 
return cell; 
}