2013-11-25 104 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"; 
     } 
    } 
} 
相关问题