[英]Tableview cell with textfields and button
我有一個tableviewcell,其中有兩個文本字段和一個按鈕。 想法是您填寫文本字段,但是該按鈕將調出另一個具有某種計算器的viewcontroller。
我的問題是將值帶回到正確的單元格和文本字段中。
我在“配置”單元格中單擊此按鈕。
[cell setDidTapButtonBlock:^(id sender) {
[self countNotes:cashoutLine.cashCurrency cell:cell];
}];
這稱為代碼摘錄;
-(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];
}
一切順利,新的ViewController出現了。 我在那里進行工作,並使用委托將總數傳回。 它很高興地到達第一個視圖控制器中,但隨后我被卡住了。 如何獲得它以填充正確的單元格?
在NotesCountViewController
- (id) initWithIndexPath:(NSIndexPath *) indexPath {
self = [super init];
if (self) {
self.indexPath = indexPath;
}
return self;
}
在您的.h中聲明它,並添加一個indexPath屬性。然后只需在您的委托回調中添加self.indexPath即可。
要調用它,只需執行以下操作:
NotesCountViewController *notesCountViewController = [[NotesCountViewController alloc] initWithIndexPath:[self.tableView indexPathForCell:cell];
然后在del回調中檢索單元格:
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
假設您要從充當UITableViewDataSource委托的ViewController中呈現計算器,則可以通過在NotesCountViewController中創建NSIndexPath屬性來找到正確的單元格來更新文本字段。 在呈現視圖控制器之前,請設置屬性。 當關閉它時,將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];
}
然后使用以下命令在Delegate回調中獲取單元格
PouchEntryViewCell *cell = (PouchEntryViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
cell.textField.text = @"New Text";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.