2013-11-25 100 views
0

我一直在第一次嘗試表格視圖,並且很難理解在執行操作時如何區分UI元素(如文本字段)細胞(例如按下按鈕)。UITableView - 更改與正在按下按鈕同一行上的文本字段

現在我在每一行都有一個按鈕和一個文本字段。爲了做到這一點:

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

    long row = [indexPath row]; 

//Button to change text field 
    strokeUp = [UIButton buttonWithType:UIButtonTypeCustom]; 
    strokeUp.frame = CGRectMake(248.0f, 11.0f, 80.0f, 32.0f); 
    [strokeUp setTag:indexPath.row]; 
    [strokeUp addTarget:self action:@selector(addStroke:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.contentView addSubview:strokeUp]; 

//Text field that changes (puts '1' in box for now) 
    strokesBox = [[UITextField alloc] initWithFrame:CGRectMake(223.0f, 11.0f, 37.0f, 32.0f)]; 
    strokesBox.delegate = self; 
    [strokesBox setTag:indexPath.row]; 
    [cell.contentView addSubview:strokesBox]; 

    return cell; 
} 

這就是我在點擊strokeUp按鈕時所做的。我希望與按鈕位於同一行的文本字段顯示'1'(稍後它會將1添加到框中的內容中)。根據用戶在前一個屏幕中選擇的行數,行數從2到6不等。

- (void)addStroke:(id)sender 
{ 
    CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:table]; 
    NSIndexPath *indexPath = [table indexPathForRowAtPoint:buttonPosition]; 
    UIButton *senderButton = (UIButton *)sender; 
    if (senderButton.tag == 0) 
    { 
     NSLog(@"Row 1"); 
     UITableViewCell *cell = [table cellForRowAtIndexPath:indexPath]; 
     UITextField *sampleField = (UITextField *)[cell viewWithTag:0]; 
     theTextField = @"1"; 
    } 
    else if (senderButton.tag == 1) { 
     NSLog(@"Row 2"); 
     UITableViewCell *cell = [table cellForRowAtIndexPath:indexPath]; 
     UITextField *sampleField = (UITextField *)[cell viewWithTag:1]; 
     sampleField.text = @"1"; 
    } 
} 
+1

它更好地使用自定義單元格,而不是像Jay Gajjar的答案那樣在表格視圖單元格中添加控件。 – Ganapathy

回答

1

設置t文本框的按鈕不同的公司說

[strokesBox setTag:indexPath.row+10]; 


// and set this code in action 


- (void)addStroke:(id)sender 
{ 
    int tag = [(UIButton *)sender tag] 
    UITextField *textField = (UITextField *)[table viewWithTag:tag+10]; 
    textField.text=[NSString stringWithFormat:@"%d",tag+1]; 
} 
+0

如果我在桌上有12個按鈕,該怎麼辦? – Rajneesh071

1

更多convinent的方法是創建一個自定義單元格,使按鈕&文本框的IBOutlets

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"gameCell"; 
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
cell.myButton addTarget:self action:@selector(addStroke:) forControlEvents:UIControlEventTouchUpInside]; 

    return cell; 
} 

然後在addStoke:方法使用

CustomCell *cell =(CustomCell) [sender superView]; 

現在,你可以操縱細胞

[email protected]"test1"; 

這裏的文本框的文本是自定義單元格的教程 http://mobile.tutsplus.com/tutorials/iphone/customizing-uitableview-cell/

+0

+1簡單而精緻的解決方案。 – Ganapathy

+2

@Ganapathy,它可能很簡單,但它不好。按鈕的超級視圖不會是單元格,而是單元格的contentView。取決於你是否在iOS 6或iOS 7中,contentView的超級視圖可能也可能不是單元格。 – rdelmar

+0

是的,我接受了。我只提到定製單元重新使用,正如我在問題評論中提到的那樣。 – Ganapathy

1

嘗試如下

第一次使用數組來設置文本框的值下面的代碼

strokesBox = [[UITextField alloc] initWithFrame:CGRectMake(223.0f, 11.0f, 37.0f, 32.0f)]; 
strokesBox.delegate = self; 
[strokesBox setTag:indexPath.row]; 
[strokesBox setText:[myMutableArray objectatindex:indexPath.row]]; 
[cell.contentView addSubview:strokesBox]; 
現在

按鈕點擊陣列和更新更改值細胞作爲波紋管

- (void)addStroke:(id)sender 
{ 
    [myMutableArray replaceObjectAtIndex:sender.tag withObject:@"1"]; 

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0]; 
NSArray *indexPaths = [[NSArray alloc] initWithObjects:indexPath, nil]; 
[self.tableView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone]; 
[indexPaths release]; 
} 
0

試試這個。

- (void)addStroke:(id)sender{ 
    UITableViewCell *cell=(UITableViewCell*)[sender superview]; 
    for (UITextField *textField in [cell subviews]) { 
     if ([textField isKindOfClass:[UITextField class]]) { 
      [email protected]"Rajneesh"; 
     } 
    } 
} 
相關問題