Ios UITableView-更改文本字段,该字段为';它与按下的按钮在同一行
我第一次尝试使用表视图,对于在单元格上执行操作(例如按下按钮)时如何区分文本字段等UI元素感到困惑 现在我在每一行上都有一个按钮和一个文本字段。为此:Ios UITableView-更改文本字段,该字段为';它与按下的按钮在同一行,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我第一次尝试使用表视图,对于在单元格上执行操作(例如按下按钮)时如何区分文本字段等UI元素感到困惑 现在我在每一行上都有一个按钮和一个文本字段。为此: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"gameCell"; gameViewCell *cell
- (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;
}
这就是我在按下冲程按钮时所做的。我希望与按钮位于同一行的文本字段显示“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";
}
}
更方便的方法是创建一个自定义单元格,并创建按钮和文本字段的
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";
以下是自定义单元的教程
更方便的方法是创建一个自定义单元格,并创建按钮和文本字段的
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";
以下是自定义单元的教程
更方便的方法是创建一个自定义单元格,并创建按钮和文本字段的
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";
以下是自定义单元的教程
更方便的方法是创建一个自定义单元格,并创建按钮和文本字段的
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";
以下是自定义单元的教程
将文本字段的标记设置为与按钮不同
[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];
}
将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];
}
将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];
}
将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];
}
尝试如下
首先使用数组在文本字段中设置值,如下代码所示
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];
}
尝试如下
首先使用数组在文本字段中设置值,如下代码所示
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];
}
尝试如下
首先使用数组在文本字段中设置值,如下代码所示
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];
}
尝试如下
首先使用数组在文本字段中设置值,如下代码所示
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];
}
试试这个
- (void)addStroke:(id)sender{
UITableViewCell *cell=(UITableViewCell*)[sender superview];
for (UITextField *textField in [cell subviews]) {
if ([textField isKindOfClass:[UITextField class]]) {
textField.text=@"Rajneesh";
}
}
}
试试这个
- (void)addStroke:(id)sender{
UITableViewCell *cell=(UITableViewCell*)[sender superview];
for (UITextField *textField in [cell subviews]) {
if ([textField isKindOfClass:[UITextField class]]) {
textField.text=@"Rajneesh";
}
}
}
试试这个
- (void)addStroke:(id)sender{
UITableViewCell *cell=(UITableViewCell*)[sender superview];
for (UITextField *textField in [cell subviews]) {
if ([textField isKindOfClass:[UITextField class]]) {
textField.text=@"Rajneesh";
}
}
}
试试这个
- (void)addStroke:(id)sender{
UITableViewCell *cell=(UITableViewCell*)[sender superview];
for (UITextField *textField in [cell subviews]) {
if ([textField isKindOfClass:[UITextField class]]) {
textField.text=@"Rajneesh";
}
}
}
最好使用自定义单元格,而不是将控件添加到表视图单元格中作为Jay Gajjar的答案。最好使用自定义单元格,而不是将控件添加到表视图单元格中作为Jay Gajjar的答案。最好使用自定义单元格,而不是将控件添加到表视图单元格中作为Jay Gajjar的答案。最好使用自定义单元格单元格,而不是像Jay Gajjar的回答那样在表视图单元格中添加控件。@Ganapathy,这可能很简单,但并不好。按钮的superview将不是单元格,而是单元格的contentView。根据您是在iOS 6还是iOS 7中,contentView的superview可能是也可能不是单元格。是的,我接受了。正如我在问题评论中提到的那样,我提到的只是为了自定义单元格的重复使用,这可能很简单,但并不好。按钮的superview将不是单元格,而是单元格的contentView。根据您是在iOS 6还是iOS 7中,contentView的superview可能是也可能不是单元格。是的,我接受了。正如我在问题评论中提到的那样,我提到的只是为了自定义单元格的重复使用,这可能很简单,但并不好。按钮的superview将不是单元格,而是单元格的contentView。根据您是在iOS 6还是iOS 7中,contentView的superview可能是也可能不是单元格。是的,我接受了。正如我在问题评论中提到的那样,我提到的只是为了自定义单元格的重复使用,这可能很简单,但并不好。按钮的superview将不是单元格,而是单元格的contentView。根据您是在iOS 6还是iOS 7中,contentView的superview可能是也可能不是单元格。是的,我接受了。正如我在问题注释中提到的,我提到的只是为了自定义单元格的重复使用。如果我在表中有12个按钮怎么办?如果我在表中有12个按钮怎么办?如果我在表中有12个按钮怎么办?如果我在表中有12个按钮怎么办?