2013-03-23 38 views
0

我爲iPad應用程序創建了UITableViewCell的子類。我需要動態生成文本字段,從用戶處獲取輸入,然後將該信息存儲在數組中。我想要問UITableViewCellUITextField.text對象,它可以容納用戶在我的View Controller的segue之前寫入的內容(我在調用segue時保存NSString對象)。所以我有一個UITableViewCells數組,我要求輸入UITextField .text對象。但由於某種原因,當我的UITableViewCell子類正在創建時,我的UITextField不是。我可以撥打UITableViewSubclass並初始化,但UITableViewSubclass.UITextField爲零。爲什麼我無法從UITableViewCell的子類中初始化UITextField?

這裏是我的UITableViewCell子類頭(是的,UITextField連接在故事板):

#import <UIKit/UIKit.h> 

@interface ConditionCell : UITableViewCell 

@property (strong, nonatomic) IBOutlet UITextField *condition; 

@end 

下面是我實現文件:

#import "ConditionCell.h" 

@implementation ConditionCell 
@synthesize condition; 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
{ 
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]; 
    if (self) { 
     // Initialization code 
     self.condition = (UITextField *)[self viewWithTag:10]; 
    } 
    return self; 
} 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated 
{ 
    [super setSelected:selected animated:animated]; 

    // Configure the view for the selected state 
} 

@end 

在這裏,這是表視圖控制器處理表包含單元格:

.h文件:

#import <UIKit/UIKit.h> 
#import "ConditionCell.h" 

@interface ConditionsTableViewController : UITableViewController 

@property (strong, nonatomic) NSMutableArray *conditionCellArray; 

- (void)addNewConditionCell; 

@end 

.m文件:

#import "ConditionsTableViewController.h" 

@interface ConditionsTableViewController() 

@end 

@implementation ConditionsTableViewController 

@synthesize conditionCellArray = _conditionCellArray; 


- (NSMutableArray *)conditionCellArray 
{ 
    if (_conditionCellArray == nil) { 
     // Create the array object 
     _conditionCellArray = [[NSMutableArray alloc] init]; 
    } 

    return _conditionCellArray; 
} 

- (void)addNewConditionCell 
{ 
    ConditionCell *condCell = [[ConditionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"conditionCell"]; 

    [self.conditionCellArray addObject:condCell]; 


    [self.tableView beginUpdates]; 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:self.conditionCellArray.count-1 inSection:0]; 
    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop]; 

    [self.tableView endUpdates]; 
    [self.tableView reloadData]; 
} 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    // Uncomment the following line to preserve selection between presentations. 
    // self.clearsSelectionOnViewWillAppear = NO; 

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    // self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return self.conditionCellArray.count; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"conditionCell"; 
    ConditionCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) { 
     cell = [[ConditionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    // Configure the cell... 
    //cell.condition = (UITextField *)[cell viewWithTag:1]; 

    return cell; 
} 


// Override to support conditional editing of the table view. 
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the specified item to be editable. 
    return YES; 
} 


// Override to support editing the table view. 
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if (editingStyle == UITableViewCellEditingStyleDelete) { 
     // Delete the row from the data source 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
    else if (editingStyle == UITableViewCellEditingStyleInsert) { 
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view 
    } 
} 


/* 
// Override to support rearranging the table view. 
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath 
{ 
} 
*/ 

/* 
// Override to support conditional rearranging of the table view. 
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Return NO if you do not want the item to be re-orderable. 
    return YES; 
} 
*/ 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Navigation logic may go here. Create and push another view controller. 
    /* 
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    */ 
} 

@end 

此表視圖控制器住一個UIView控制器內的表視圖不佔用整個屏幕。當用戶按下'ok'按鈕時,會觸發一個segue,在這裏我要求這個Table View Controller包含UITableViewCells的數組,然後我通過foreach運行它以獲取它們的.text屬性。不幸的是,我似乎無法獲得任何我輸入到文本字段的內容,因此.text的值始終爲零。如果有人可以幫助我解決這個問題,將不勝感激!

+1

在您的第一個.m文件中,如果故事板中連接了「condition」的IBOutlet,爲什麼要在初始化方法中設置其值? – Isaac 2013-03-23 03:57:56

+0

如果你把你的單元放在IB中,爲什麼要使用initWithStyle ...?另外我敢打賭,保持一個表格單元陣列不是你想要做的。你可能想要一個字符串數組。 – 2013-03-23 05:41:17

+0

initWithStyle現在正在給我帶來很多痛苦。我應該把一個init方法放在單元的.m文件中,然後調用它嗎? – Tino 2013-03-23 14:21:07

回答

0

您可能會發現使用免費的Sensible TableView框架更容易。該框架具有這些文本字段單元格,甚至可以從數組中自動創建它們。

0

我想出了一個更好的方式來做我想做的事情。事實證明,iOS的UITableView的工作方式與我想要做的完全不同。 UITableView通過查看故事板並給出單元格的標識符來工作,它創建它們並允許您在cellForRowAtIndexPath方法中設置它們的屬性。但是,當單元格離開屏幕時,它不會保留,因爲它是自己單獨的對象;它被重用。所以,你可以把它看作是當你滾動表格視圖時,消失到一端的單元格會在新的信息的另一端再次出現。這是關鍵 - UITableView希望你提供單元的信息。它不是直接在UITableViewCell上輸入信息的,這是我想要做的。

所以我落得這樣做是複製粘貼我的細胞轉化爲自己的.xib文件,並在子類initWithStyle:reuseIdentifier方法,這樣做:

NSArray *nibArray = [[NSBundle mainBundle] loadNibNamed:@"ConditionCell" owner:self options:nil]; 
     self = [nibArray objectAtIndex:0]; 

而這與任何風格創建細胞 - 設置 - 你想要的UI元素。

接下來,我想堅持對單元格的引用,因爲那個單元格有一個文本框,當用戶按下「完成」按鈕時,我需要保存文本框上的內容。然而,測試揭示了我上面解釋的重用問題。那麼如何做到這一點?我的表中的視圖控制器,只要用戶想增加一個新的文本框(和按下按鈕這樣做),我有做

[self.conditionCellArray insertObject:[[ConditionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"conditionCell"] atIndex:0]; 

這增加了數組的新細胞的方法 - 這是因爲重要我需要一直參考所有細胞。 (它將在索引0處添加單元格,因爲我想將其插入頂部)。然後,在cellForRowAtIndexPath方法中,我做了

return [self.conditionCellArray objectAtIndex:indexPath.row]; 

這將返回相應的單元格。請記住,從我已經讀過的所有關於保持對錶中每個單元格的引用的全部內容與Apple使用UITableView時指出的最佳實踐相反。但是,正如我之前所說的,UITableView是爲了顯示信息,而不是從用戶輸入中收集信息。所以這就是爲什麼我不得不違反規則,如果你願意,達到預期的效果(我想要的)。我希望這能幫助那些想要做同樣事情的人;如果有更好的方法,不要害羞告訴我。

編輯:哦,順便說一句,當你複製粘貼故事板中創建的單元格到他們自己的.xib文件時,請務必斷開任何IBOutlets並將它們的類更改回UITableViewCell。這樣,連接.xib文件單元時就不會出現任何問題或衝突。

相關問題