我有一个自定义的tableview单元格,其中包含文本字段和两个标签
TableViewCell:
@interface CartTableViewCell : UITableViewCell
@property (weak, nonatomic) IBOutlet UILabel *proCost;
@property (weak, nonatomic) IBOutlet UITextField *txtQtyCount;
@property (weak, nonatomic) IBOutlet UILabel *lblTotalCost;
@end
ViewController.h
@interface CartViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UITabBarControllerDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tblView;
@end
ViewController.m
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
CGPoint origin = textField.frame.origin;
CGPoint point = [textField.superview convertPoint:origin toView:self.tblView];
NSIndexPath * indexPath = [self.tblView indexPathForRowAtPoint:point];
NSLog(@"%ld",(long)cartCell.txtQtyCount.tag);
[self.tblView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
qty = cartCell.txtQtyCount.text;
quanty = [qty doubleValue];
double qua = price * quanty;
qtotal =[[NSString alloc] initWithFormat:@"%.2f",qua];
[dictionary setObject:qtotal forKey:@"qtotal"];
[dictionary setObject:qty forKey:@"qty"];
[self.tblView beginUpdates];
NSArray *indexPaths = [[NSArray alloc] initWithObjects:indexPath, nil];
[self.tblView reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
[self.tblView endUpdates];
[cartCell.txtQtyCount resignFirstResponder];
return YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
cartCell =[self.tblView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
NSMutableString *name;
name = [NSMutableString stringWithFormat:@"%@ ",
[tmpDict objectForKey:@"name"]];
qty = [NSMutableString stringWithFormat:@"%@ ",
[tmpDict objectForKey:@"qty"]];
NSString *qtot = [NSString stringWithFormat:@"%@",[tmpDict objectForKey:@"qtotal"]];
NSMutableString *qcost;
qcost = [NSMutableString stringWithFormat:@"%@ ",
[tmpDict objectForKey:@"total"]];
cartCell.txtQtyCount.tag = indexPath.row;
cartCell.proCost.text = qcost;
cartCell.txtQtyCount.text = qty;
cartCell.lblTotalCost.text = qtot;
}
实际上我有一个proCost
,其中包含一个双倍值txtQtyCount
,我可以更新数量,以便我将价格与数量相乘,并希望将相乘值保存在{{1}中}。我不清楚如何获取已编辑行的值(索引路径)。
答案 0 :(得分:0)
只需查看更新的数据源数组(myObject),即可检索所需的任何值。
显然你需要保持更新,所以当用户输入新数量时,请确保以相应的索引更新“qty”tmpDict。
答案 1 :(得分:0)
对此有类似的问题。我认为我真正需要的是正确单元格中该视图的indexPath
所以..我做了一个UITakeView
category
并且有这样的方法:
-(NSIndexPath *)indexPathForCellContaininView:(UIView *)view
{
while (view != nil){
if ([view isKindOfClass:[UITableViewCell class]]){
return [self indexPathForCell:(UITableViewCell *)view];
}else {
view = [view superview];
}
}
return nil;
}
然后你可以在对你的用例有意义的UITextFields
代表中使用它 - 启用:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSIndexPath *indexPath = [self.tableView indexPathForCellContaininView:textField];
/// do something with the index path - you have the exact cell / uitextfield being edited //
return YES;
}