有人可以帮我这个吗?我一直在谷歌搜索很长时间,找不到任何答案.
SaveVIEwController.h:
#import <UIKit/UIKit.h>#import "RecordsVIEwController.h" //Next VIEwController@interface SaveVIEwController : UItableVIEwController <UItableVIEwDataSource,UItableVIEwDelegate> {__weak IBOutlet UITextFIEld *runnameFIEld;RecordsVIEwController *RecordsVcdata;}@property (weak,nonatomic) IBOutlet UITextFIEld *runnameFIEld;@property (weak,nonatomic) IBOutlet UITextVIEw *runnotesTextVIEw;@property (weak,nonatomic) IBOutlet UIbarbuttonItem *savebarItem;@property (nonatomic,retain) RecordsVIEwController *RecordsVcdata;- (IBAction)saverun:(UIbarbuttonItem *)sender;- (IBAction)goAwayKeyboard:(ID)sender;@end
SaveVIEwController.m:
- (IBAction)saverun:(UIbarbuttonItem *)sender {RecordsVIEwController *RecordsVC = [[RecordsVIEwController alloc] initWithNibname:nil bundle:nil];RecordsVC.tableVIEw = [[UItableVIEw alloc] initWithFrame:CGRectZero style:UItableVIEwStyleGrouped];self.RecordsVcdata = RecordsVC;[RecordsVC.runsArray addobject:[Nsstring stringWithFormat:@"%@",runnameFIEld.text]];[self presentModalVIEwController:RecordsVC animated:YES];}- (IBAction)goAwayKeyboard:(ID)sender { [sender resignFirstResponder];}
RecordsVIEwController.h:
#import <UIKit/UIKit.h>@interface RecordsVIEwController : UItableVIEwController <UItableVIEwDataSource,UItableVIEwDelegate> { NSMutableArray *runsArray;}@property (weak,nonatomic) Nsstring *runname;@property (strong,nonatomic) Nsstring *runnotes;@property (weak,nonatomic) UItableVIEwCell *cell;@property (retain,nonatomic) NSMutableArray *runsArray;@end
RecordsVIEwController.m:
-(UItableVIEwCell *)tableVIEw:(UItableVIEw *)tableVIEw cellForRowAtIndexPath: (NSIndexPath *)indexPath {// IDentifIEr for retrIEving reusable cells.static Nsstring *cellIDentifIEr = @"MyCellIDentifIEr";// Attempt to request the reusable cell.UItableVIEwCell *cell = [tableVIEw dequeueReusableCellWithIDentifIEr:cellIDentifIEr];cell = [[UItableVIEwCell alloc]initWithStyle:UItableVIEwCellStyleDefault reuseIDentifIEr:cellIDentifIEr];// No cell available - create oneif(cell == nil) { cell = [[UItableVIEwCell alloc] initWithStyle:UItableVIEwCellStyleDefault reuseIDentifIEr:cellIDentifIEr];[cell.textLabel setText:[Nsstring stringWithFormat:@"%@",[runsArray objectAtIndex:indexPath.row]]];}// Set the text of the cell to the runnamestring.[cell.textLabel setText:[Nsstring stringWithFormat:@"%@",[runsArray objectAtIndex:indexPath.row]]];return cell;}- (NSInteger)tableVIEw:(UItableVIEw *)tableVIEw numberOfRowsInSection:(NSInteger)section {return [runsArray count];}
任何帮助都很受欢迎.
谢谢,
Dhruv直升机
解决方法 在你更新RecordsVC.runsArray的时候,我想你的tablevIEw已经被初始化和加载了,虽然没有数据.我最近在这种情况下所做的是使用通知.
在RecordsVIEwController中,您将在init方法中注册通知:
- (ID)initWithNibname:(Nsstring *)nibnameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibname:nibnameOrNil bundle:nibBundleOrNil]; if (self) { [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(dataLoaded:) name:@"runsArrayLoaded" object:nil]; // Custom initialization } return self;}
同样在RecordsVIEwController中,您将需要在收到通知时调用的方法.在这种情况下,它是dataLoaded:
-(voID) dataLoaded:(NSNotification *)notif { if (notif.userInfo){ runsArray = [notif.userInfo objectForKey:@"newdata"]; [self.tablevIEw reloadData]; }}
使所有这些神奇工作的部分在SaveVIEwController类中的saverun方法中.
- (IBAction)saverun:(UIbarbuttonItem *)sender { RecordsVIEwController *RecordsVC = [[RecordsVIEwController alloc] initWithNibname:nil bundle:nil]; RecordsVC.tableVIEw = [[UItableVIEw alloc] initWithFrame:CGRectZero style:UItableVIEwStyleGrouped]; [runsArray addobject:[Nsstring stringWithFormat:@"%@",runnameFIEld.text]]; [self presentModalVIEwController:RecordsVC animated:YES]; // notify the RecordsVIEwController that new data is available NSMutableDictionary* userInfo = [[NSMutableDictionary alloc] initWithCapacity:1]; [userInfo setobject:runsArray forKey:@"newdata"]; [[NSNotificationCenter defaultCenter] postNotificationname:@runsArrayLoaded" object:nil userInfo:(NSDictionary*)userInfo];}
最后,您必须清理并删除RecordsVIEwController类中的通知观察.在vIEwWilldisappear中,添加以下行:
[[NSNotifcation defaultCenter] removeObserver:self name:@"runsArrayLoaded" object:nil];总结
以上是内存溢出为你收集整理的nsmutablearray – 从先前视图控制器中的数组获取数据,将其放入另一个TableViewController中全部内容,希望文章能够帮你解决nsmutablearray – 从先前视图控制器中的数组获取数据,将其放入另一个TableViewController中所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)