2011-01-24 87 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; 
}