UITableViewCell文本字段值在另一种方法中

时间:2017-01-09 08:21:03

标签: ios uitableview uitextfield tableviewcell

-(void)sendcomment:(UIButton *)sender{

    NSLog(@" send commment server");

    thirdviewcellTableViewCell *dja =[[thirdviewcellTableViewCell alloc] init];
    NSString *vga = [[NSString alloc] init];
    vga=dja.celltext;
    NSLog(@"comment value is %@",vga);
    NSLog(@"comment cell value is %@",dja.celltext);
}

4 个答案:

答案 0 :(得分:1)

-(void)sendcomment:(UIButton *)sender
 {
 UIButton *button = (UIButton*)sender;
 NSIndexPath *indexpath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
 UITableViewCell *cell = (UITableViewCell*)[tblView cellForRowAtIndexPath: indexpath];
  // If you have text field
 UITextField *textfiled = (UITextField *)[cell.contentView viewWithTag:yourtextfeildtagvlaue];

// NSString *vga = textfiled.text
 }

答案 1 :(得分:0)

如果单元格按住按钮,则可以执行以下操作:

-(void)sendcomment:(UIButton *)sender{

    // 1. get the position the button you clicked
    CGPoint correctedPoint = [sender convertPoint:button.bounds.origin toView:self.tableView];

    // 2. find the cell via the position
    NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:correctedPoint];
    thirdviewcellTableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];;

    // 3. then you can get the TextField value
    NSString *textFieldValue = [[NSString alloc] init];
    textFieldValue = cell.celltext;
}

答案 2 :(得分:0)

使用单元格的层次结构

获取您的单元格
 -(void)sendcomment:(UIButton *)sender
     {
     UIButton *button = (UIButton*)sender;
     NSIndexPath *indexpath = [NSIndexPath indexPathForRow:sender.tag inSection:0];
     UITableViewCell *cell = (UITableViewCell*)sender.superview.superview;// get cell using view hierarchy 
      // If you have text field
     UITextField *textfiled = (UITextField *)[cell.contentView viewWithTag:yourtextfeildtagvlaue];
    // NSString *vga = textfiled.text
     }

*

答案 3 :(得分:0)

在您的操作方法中,您可以找到您的TextField,如下所示: 您可以从SuperView中找到IndexPath。 见下面的代码。

UIView *superView = btn.superview;

while (![superView isKindOfClass:[UITableViewCell class]]) {
    superView = superView.superview;
}

CustomCell *cell = (CustomCell *)superView;

您将从UITextField获得cell.yourTextFeld个对象。 您可以根据自己的要求使用TextField。