2010-12-07 40 views
0

我是noob。我需要將UITextView插入到具有動態調整大小的UITableViewCell中,並且我將直接輸入到單元格中。請幫我解決這個問題。UITextView到UITableViewCell中

+0

您應該將Rog的答案標記爲已接受,以便此問題已關閉。 (你和Rog都會被授予聲望點。) – 2010-12-08 13:50:50

回答

0

這不是標準的UITableView設計的工作方式。如果這是您想要實現的目標,則可以添加/編輯/刪除項目。

我建議您閱讀Table View Programming Guide for iOS(特別是「插入和刪除行和部分」一節),因爲這會使您走上正確的軌道。

你當然可以創建自定義視圖等,如果你真的允許用戶鍵入一個細胞,但作爲一個自認「小白」我不會建議嘗試此,直到你更有信心用上述方法等

+1

我不一定會同意這一點。如果您正在爲用戶輸入表單以實現表單?使用UITextFields的tableview絕對是一個很好的解決方案,實際上它已被Apple使用了很多(例如當你在iPhone上添加新的聯繫人時)。 – Rog 2010-12-07 21:50:54

+0

我只是想讓初學者在創建自定義視圖之前瞭解標準的做事方式是一個很好的起點。 – 2010-12-07 21:52:03

7

你需要它爲UITextField子類的UITableViewCell:

@interface CustomCell : UITableViewCell { 
    UILabel *cellLabel; 
    UITextField *cellTextField; 
} 

@property (nonatomic, retain) UILabel *cellLabel; 
@property (nonatomic, retain) UITextField *cellTextField; 

@end 

,然後實現:

@implementation CustomCell 
@synthesize cellLabel, cellTextField; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 

     cellLabel = [[UILabel alloc] initWithFrame:CGRectZero]; 
    ... // configure your label appearance here 

     cellTextField = [[UITextField alloc] initWithFrame:CGRectZero]; 
     ... // configure your textfield appearance here 

    } 

    return self; 
} 

最後使用您的自定義單元格:

- (CustomCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    ... // configure your cell data source here 
    return cell; 
} 
相關問題