2009-02-23 29 views
0

我基本上試圖在tableView上添加一個UITextField到UITableViewCell:commitEditingStyle:forRowAtIndexPath :.在插入時,我想在現有的單元格上添加一個UITextField。iPhone:用UITextField替換單元格的視圖

我設置的是這樣的文本字段:

-(void) setUpIndexField { 
    inputField = [[UITextField alloc] init]; 
    inputField.textAlignment = UITextAlignmentCenter; 
    inputField.borderStyle = UITextBorderStyleRoundedRect;  
    inputField.keyboardType = UIKeyboardTypeNamePhonePad; 
    inputField.delegate = self; 
} 

然後在commitEditingStyle,我有一些如:

- (void)tableView:(UITableView *)tv 
    commitEditingStyle:(UITableViewCellEditingStyle)editingStyle 
    forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSInteger row = [indexPath row]; 
    if (editingStyle == UITableViewCellEditingStyleInsert) { 
    if (row == 0) { 
     UITableViewCell *cell = [tv cellForRowAtIndexPath:indexPath]; 
     inputField.frame = [tv cellForRowAtIndexPath:indexPath].frame; 
     [cell.contentView addSubview:inputField]; 
     [inputField becomeFirstResponder]; 
     return; 
    } 
// more code etc. 
} 

當我點擊插入存取(+),文本字段確實會被淹沒在屏幕上,但沒有靠近單擊它的地方。它會進入UIView的隨機部分,而不是在點擊+處的indexPath的實際單元格。

瞭解我出錯的地方,以及如何將UITextField放置在單擊「+」的單元格上將是理想選擇。

回答

1

您正在將inputField的幀設置爲單元格的框架,該框架不會按照您的要求進行操作。單元格內的視圖使用框架的左上角作爲0,0。你的文本框框架應該是:

inputField.frame = CGRectMake(0, 0, cell.frame.size.width, cell.frame.size.height); 

自認爲是相對於其上海華將放置文本框,因此它完全覆蓋了單元格中的其他意見。

+0

嗨擠,這差不多就是這樣。現在唯一的另一個問題是單元格的寬度是320,所以當UITableView的樣式被舍入時,它會延伸過單元格。無論如何,它仍然在UITableViewCell的範圍內,而不是繼續之後? – Coocoo4Cocoa 2009-02-23 21:57:31

相關問題