2016-02-29 75 views
0

在TagViewController.m我從cellview.xib添加標籤的文本字段沒有得到第一的UITextField的值從UITableView的細胞

(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"SimpleTableCell"; 

    TagCell *cell = (TagCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"TagCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 
    } 

    cell.txtCellTag.tag = indexPath.row; 
    return cell; 
} 

在按鈕點擊我試圖讓文本框的值下面的功能

(IBAction)btnSave:(id)sender { 

    for(int i=0;i<[dataId count];i++) { 

     UITableViewCell *cell = [[self tblImages] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; 
     if(cell == nil) { 
      NSLog(@";Cell is nil"); // Cell is always nil 
     } else { 
      UITextField *textField = (UITextField *)[cell viewWithTag:i]; 
      NSLog(@"Value is %@", textField.text); 
     } 
    } 
} 

我得到的所有文本字段的值,除非第一行 第一個字段的值始終爲NULL來

+1

行關閉屏幕?在細胞中存儲狀態並不是一個好主意,因爲它們在屏幕上移動和移出時會被回收並丟棄。用戶輸入時應將值傳遞給視圖控制器。您的單元格和視圖控制器之間的委託方法通常適用於傳遞像這樣的數據。 –

回答

0

做得更好類型強制轉換的UITableViewCell的d從單元格中獲取文本字段。

for(int i=0;i<[dataId count];i++) { 

    UITableViewCell *cell = (TagCell *)[[self tblImages] cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]]; 
    if(cell == nil) { 
     NSLog(@";Cell is nil"); // Cell is always nil 
    } else { 
     UITextField *textField = cell.txtCellTag; 
     NSLog(@"Value is %@", textField.text); 
    } 
}