2013-03-24 66 views
1

我想在我的UITextField中檢索輸入的數據並將其分配給參數。我試圖指定標籤的文本框,但標籤全部變成0檢索UITableView中的UITextField數據

代碼:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString * defaultCellId = @"DefaultCell"; 
    static NSString * fieldCellId = @"FieldCell"; 
    static NSString * pictureCellId = @"PictureCell"; 

    DefaultCell *defaultCell = [_tableView dequeueReusableCellWithIdentifier:defaultCellId]; 
    FieldCell *fieldCell = [_tableView dequeueReusableCellWithIdentifier:fieldCellId]; 
    PictureCell *pictureCell = [_tableView dequeueReusableCellWithIdentifier:pictureCellId]; 

    if (indexPath.section == 1) { 

     if (fieldCell == nil) { 
      fieldCell = [[FieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:fieldCellId]; 
     } 

     fieldCell.field.tag = indexPath.row + 1; //**THIS LINE** 

     if (indexPath.row == 0) 
      fieldCell.label.text = @"Namn"; 
     if (indexPath.row == 1) 
      fieldCell.label.text = @"E-post"; 
     if (indexPath.row == 2) 
      fieldCell.label.text = @"Telefon"; 
     return fieldCell; 

    } else if (indexPath.section == 2) { 

     if (defaultCell == nil) { 
      defaultCell = [[DefaultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:defaultCellId]; 
     } 
     defaultCell.label.text = @"Plats"; 
     return defaultCell; 

    } else if(indexPath.section == 3) { 

     if (fieldCell == nil) { 
      fieldCell = [[FieldCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:fieldCellId]; 
     } 
     if(indexPath.row == 0) 
      fieldCell.label.text = @"Titel"; 
     if(indexPath.row == 1) 
      fieldCell.label.text = @"Text"; 
     return fieldCell; 

    } else if(indexPath.section == 4) { 

     if (pictureCell == nil) { 
      pictureCell = [[PictureCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:pictureCellId]; 
     } 
     pictureCell.label.text = @"Bild"; 
     return pictureCell; 

    } else { 

     if (defaultCell == nil) { 
      defaultCell = [[DefaultCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:defaultCellId]; 
     } 
     defaultCell.label.text = @"Lägg upp annons"; 
     return defaultCell; 
    } 
} 

It's像這樣的textFieldDidEndEditing方法我想要的。

- (void)textFieldDidEndEditing:(UITextField *)textField { 

    if(textField.tag == 1) 
     NSString *title = textField.text; 
    if(textField.tag == 2) 
     NSString *phone == textField.text; 
} 

感謝幫助,謝謝。

回答

0

而不是使用三個不同的自定義單元格使用單個單元格與其中的所有三個組件。 並分配一個標籤號,而在的cellForRowAtIndexPath方法

CueTableCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

if (cell == nil) { 
    NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"CueTableCell XibName" owner:self options:nil]; 
    // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). 
    cell = [array objectAtIndex:0]; 

    // Set tag to textField here and implement Delegate method of it to read value 
    [cell.textField setTag:indexPath.row]; 
} 
0

創建細胞對於多UITextFields表視圖,我絕對推薦免費的明智的TableView框架。在你的情況下,框架會自動從你的對象的屬性創建字段,然後它也會自動設置他們的值,無論用戶輸入什麼。我發現它非常方便。

+0

謝謝,我會看看! – 2013-03-25 13:50:08