2011-03-15 107 views
2

我想按下QTY按鈕(紅色文本)並將文本(即13)複製到同一行中的文本域。幫助! uitableviewcell按鈕更新文本域

here is my sample uitableviewcell

-(IBAction)qtyButtonPressed:(id)sender { 

UITextField *textField = (UITextField *)[self.view viewWithTag:3]; 

textField.text = @"13"; 

這是我有個大氣壓。

+0

如果您發佈相關代碼,您可能會得到更多答案。 – sarnold 2011-03-15 23:53:05

回答

1

如果每個單元格都有一個按鈕,首先您需要能夠識別哪個按鈕從哪個行被點擊。通常,如果它與1節表格,你可以設置的行號內的cellForRowAtIndexPath按鈕標記值:...設置在小區可見

[button setTag:indexPath.row]; 

然後在選擇調用的時候,按鈕壓制,取得標記值來確定行號,並設置文本框的文本行

int row = [sender tag]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row section:0]; 
    id cell = [tableView cellForRowAtIndexPath:indexPath]; 
    [cell.textField setText:....]; 

在對於這個工作,你需要繼承一個UITableViewCell,使按鈕和文本框財產/合成訪問。

+0

非常感謝honcheng! – johnstontrav 2011-03-16 01:53:11

0

您可以在按鈕上使用addTarget:action:forControlEvents:並使用UIControlEventTouchUpInside來註冊一個選擇器,該按鈕被觸摸時會被調用。然後在該方法中找到相應的文本字段並分配其text屬性。

+0

感謝Anomie,我已經這樣做了,但我很難「找到」相應的文本字段。有什麼幫助嗎? – johnstontrav 2011-03-16 00:21:12

1

我知道這已被回答,但我有一個類似的問題,不幸的是我已經使用標籤來查找表視圖單元格內的字段,讓我把它放在InterfaceBuilder/Xcode中,並仍然避免這樣的子類化:

- (UITableViewCell *) tableView: (UITableView *) tableView cellForRowAtIndexPath: (NSIndexPath *) indexPath { 
    static NSString *AttributeCellIdentifier = @"AttributeCell"; 
    UITableViewCell *cell; 
    UILabel *label; 
    UITextField *value; 
    MyAttribute *a; 

    switch(indexPath.section) { 
     case ATTRIBUTES_SECTION: 
      cell = [tableView dequeueReusableCellWithIdentifier: AttributeCellIdentifier]; 
      label = (UILabel *) [cell viewWithTag: 1]; 
      value = (UITextField *) [cell viewWithTag: 2]; 
      a = [attributeList objectAtIndex: indexPath.row]; 
      label.text = a.label; 
      value.text = a.value; 
      break; 
     // Other sections... 
    } 
    return cell; 
} 

但這意味着我不能使用標籤的行的文本框是那麼作爲替代使用標籤我使用文本字段的座標,看看排它是這樣的:

- (void) textFieldDidEndEditing: (UITextField *) textField { 
    NSLog(@"Entering %s with %@", __func__, textField); 
    NSIndexPath *textFieldLocation = [self.tableView indexPathForRowAtPoint: [textField convertPoint:textField.bounds.origin toView: self.tableView]]; 
    NSLog(@"- The textfield is in the cell at: %@", textFieldLocation); 

    if(textFieldLocation.section == ATTRIBUTES_SECTION) { 
     MyAttribute *a = [attributeList objectAtIndex: textFieldLocation.row]; 
     a.value = textField.text; 
    } 
} 

如果我有多個文本字段w在單元格中,我仍然可以使用標記值來知道哪一個結束編輯。

它甚至可能是明智的建立,返回的tableview指數爲任一視圖的小幫手方法:

- (NSIndexPath *) indexPathForView: (UIView *) view { 
    NSIndexPath *loc = [self.tableView indexPathForRowAtPoint: [view convertPoint: view.bounds.origin toView: self.tableView]]; 
    return loc; 
} 

這可以在一個類別提出和容易獲得任何的tableview而不需要編寫任何代碼。