我有一个UITableView
,每个UITableViewCell
都有一个文本字段和UILabel
。如何在编辑相应单元格中的文本字段时更新单元格上的标签字符串。
我尝试过类似的东西,但它没有用
UITextField *myTextField = (UITextField *)[cell viewWithTag:100];
UILabel *myLabel = (UILabel *)[cell viewWithTag:101];
[myTextField addTarget:self action:@selector(textFieldEdited:event:) forControlEvents:UIControlEventEditingDidEnd];
- (void)textFieldEdited:(UITextField*)textfield event:(UIEvent*)event
{
NSIndexPath* indexPath = [myTable indexPathForRowAtPoint:
[[[event touchesForView:textfield] anyObject]
locationInView:cartTable]];
NSLog(@"%d",indexPath.row);
UILabel *myLabel = (UILabel *)[[myTable cellForRowAtIndexPath:indexPath] viewWithTag:101];
[myLabel setText:@"44"];
}
但它不起作用。
答案 0 :(得分:1)
试试这个..这可能会对你有所帮助。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
UITableviewCell *currentCell = (UITableviewCell *) [textField superView];
UILabel *label = [currentCell viewWithTag:YOURLABELTAG];
label.text = [NSString stringWithFormat:@"%@%@",textField.text,string];
return YES;
}
注意:不要忘记设置textFieldDelegate
并设置label Tag
答案 1 :(得分:0)
在textFieldEdited中:更新数据源并重新加载表数据
[myTable reloadData];
然后处理更改,然后在您的cellForRowAtIndexPath:方法中返回更新的单元格。
答案 2 :(得分:0)
CGPoint location = [tableview convertPoint:yourtextfield.frame.origin fromView:yourtextfield.superview];
NSIndexPath *indexPath = [tblItemList indexPathForRowAtPoint:location];
UITableview *cell = [self.table cellForRowAtIndexPath:indexPath];
for (UIView *subView in cell.subviews)
{
if ([subView isKindOfClass:[UILabel class]])
{
[(UILabel *) subView setText:@"Hearts"];
}
}
答案 3 :(得分:0)
查找单元格的索引路径是简单的一行代码
NSIndexPath * indexPath = [tableView indexPathForCell:cell];