gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 20:11:12 25 4
gpt4 key购买 nike

我是第一次尝试使用表格 View ,对于在对单元格执行操作(例如按下按钮)时如何区分文本字段等 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";
}
}

最佳答案

设置textfield标签和button say不同

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

关于ios - UITableView - 更改与按下的按钮位于同一行的文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20185222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com