我已经研究了很多关于如何做到这一点的问题,而且我的工作时间很短。
我有ViewControllerA和ViewControllerB。
ViewControllerB
正在将NSDate
从UIDatePicker
传递回ViewControllerA
。
我很好,直到尝试将NSDate
作为标签放在与其对应的TableViewCell
中。
ViewControllerA
- (void)addItemViewController:(EXEDurationPickerViewController *)controller didFinishEnteringItem:(NSString *)item {
NSLog(@"This was returned from ViewControllerB %@", item);
}
item
是从ViewControllerB中选择的日期。如何让它在相应的TableViewCell中显示为标签?
答案 0 :(得分:1)
使用delegate
传递日期或其他选项是发送Notificaition
在ViewControllerA
@interface ViewControllerA : UIViewController{
NSIndexPath *selectedIndexPath;
}
@end
-(void)viewDidLoad{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(receiveNotification:)
name:@"dateSend"
object:nil];
}
- (void) receiveNotification:(NSNotification *) notification
{
NSString *item = notification.userInfo[@"date"];
// show for what cell you want to show
//keep selectedIndexPath as instance Variable
YourCell *cell = (YourCell *)[self.tableView cellForRowAtIndexPath:selectedIndexPath];
cell.label.text = item;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
selectedIndexPath = indexPath;
}
//Post the notification fro `ViewControllerB`
- (void)addItemViewController:(EXEDurationPickerViewController *)controller didFinishEnteringItem:(NSString *)item {
NSLog(@"This was returned from ViewControllerB %@", item);
[[NSNotificationCenter defaultCenter] postNotificationName: @"TestNotification" object:nil userInfo:@{@"date":item}];
}
答案 1 :(得分:1)
在didSelectRowAtIndexPath中(或者在prepareForSegue中,如果您正在使用它)在属性中保存所选单元格的indexPath。然后,在您的委托方法中,将项添加到模型中(无论您使用哪个填充表视图),然后使用保存的indexPath调用reloadRowsAtIndexPath:来更新表。