UITableView - 更改与按下按钮位于同一行的文本字段

时间:2013-11-25 05:15:35

标签: ios objective-c uitableview

我第一次尝试使用表格视图,并且在对单元格采取操作(例如按下按钮)时如何区分文本字段等UI元素时感到困惑。

现在我每行都有一个按钮和一个文本字段。为此:

- (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";
    }
}

4 个答案:

答案 0 :(得分:1)

更有说服力的方法是创建一个自定义单元格并制作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];

现在您可以操作单元格的文本字段文本

cell.textField1.text=@"test1";

以下是自定义单元格的教程 的 http://mobile.tutsplus.com/tutorials/iphone/customizing-uitableview-cell/

答案 1 :(得分:1)

设置textfield的标签与按钮说

不同
    [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];
}

答案 2 :(得分:1)

尝试以下

首先使用数组在textfield中设置值,如下面的代码

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];
}

答案 3 :(得分:0)

试试这个。

- (void)addStroke:(id)sender{
    UITableViewCell *cell=(UITableViewCell*)[sender superview];
    for (UITextField *textField in [cell subviews]) {
        if ([textField isKindOfClass:[UITextField class]]) {
            textField.text=@"Rajneesh";
        }
    }
}