简体   繁体   English

带有文本字段和按钮的Tableview单元格

[英]Tableview cell with textfields and button

I have a tableviewcell that has two text fields and a button. 我有一个tableviewcell,其中有两个文本字段和一个按钮。 The idea is that you fill in the text field, but the button will bring up another viewcontroller that has some sort of calculator. 想法是您填写文本字段,但是该按钮将调出另一个具有某种计算器的viewcontroller。

My problem comes with bringing the value back into the right cell and textfield. 我的问题是将值带回到正确的单元格和文本字段中。

I get at the button click with this in the Configure cell; 我在“配置”单元格中单击此按钮。

[cell setDidTapButtonBlock:^(id sender) {
    [self countNotes:cashoutLine.cashCurrency cell:cell];

}];

This calls this extract of code; 这称为代码摘录;

-(void)countNotes:(Currency *)cashCurrency cell:(PouchEntryViewCell *)cell
{

    NotesCountViewController *notesCountVC = [[NotesCountViewController alloc] init];
    notesCountVC.noteCurrency = cashCurrency;

    notesCountVC.notesDelegate = self;

     notesCountVC.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController:notesCountVC animated:YES completion:nil];
}

All good and the new viewcontroller shows up. 一切顺利,新的ViewController出现了。 I do the work there and pass back the total using delegate. 我在那里进行工作,并使用委托将总数传回。 It arrives happily in the first view controller, but then I'm stuck. 它很高兴地到达第一个视图控制器中,但随后我被卡住了。 How can I get it to populate the correct cell? 如何获得它以填充正确的单元格?

In NotesCountViewController NotesCountViewController

- (id) initWithIndexPath:(NSIndexPath *) indexPath {

    self = [super init];

    if (self) {
        self.indexPath = indexPath;
    }

    return self;
}

Declare this in your .h and also add an indexPath property.. Then just add the self.indexPath in your delegate call back. 在您的.h中声明它,并添加一个indexPath属性。然后只需在您的委托回调中添加self.indexPath即可。

To call it just do: 要调用它,只需执行以下操作:

NotesCountViewController *notesCountViewController = [[NotesCountViewController alloc] initWithIndexPath:[self.tableView indexPathForCell:cell];

Then to retrieve the cell in the del callback: 然后在del回调中检索单元格:

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

Assuming you're presenting the calculator from the ViewController that acts as the UITableViewDataSource delegate, you can find the correct cell to update the textfield by creating a NSIndexPath property in your NotesCountViewController. 假设您要从充当UITableViewDataSource委托的ViewController中呈现计算器,则可以通过在NotesCountViewController中创建NSIndexPath属性来找到正确的单元格来更新文本字段。 Before you present the view controller, set the property. 在呈现视图控制器之前,请设置属性。 When it is dismissed, pass the NSIndexPath back to your UITableViewDataSource viewController, and find the correct cell with that. 当关闭它时,将NSIndexPath传递回UITableViewDataSource viewController,并使用它找到正确的单元格。

-(void)countNotes:(Currency *)cashCurrency cell:(PouchEntryViewCell *)cell atIndex:(NSIndexPath *)indexPath
{
    NotesCountViewController *notesCountVC = [[NotesCountViewController alloc] init];
    notesCountVC.noteCurrency = cashCurrency;

    notesCountVC.cellIndexPath = indexPath;

    notesCountVC.notesDelegate = self;
    notesCountVC.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController:notesCountVC animated:YES completion:nil];
}

Then get the cell in the Delegate callback with 然后使用以下命令在Delegate回调中获取单元格

PouchEntryViewCell *cell = (PouchEntryViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
cell.textField.text = @"New Text";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM