gpt4 book ai didi

ios - 未调用 cellForRowAtIndexPath

转载 作者:行者123 更新时间:2023-12-01 18:02:06 26 4
gpt4 key购买 nike

我在两个 View 之间沟通时遇到了麻烦。我的目标是向我的 NSMutableArray 添加一个新对象.感谢 stackoverflow 上可爱的人,他们已经修复了。现在我有另一个问题。
即使我能够将对象添加到我的 NSMutableArray ,表不会更新自身以使用新数据重新填充。这是一些代码:

第一 View Controller .h

@interface FirstViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>{  
IBOutlet UITableView *mytableview;
NSMutableArray *mytableinfo;
@property (nonatomic,retain) IBOutlet UITableView *mytableview;
@property (nonatomic,retain) NSMutableArray *mytableinfo;`

第一 View Controller .m
-(IBAction)addShift:(id)sender{
SecondViewController *secondViewController = [[SecondViewController alloc]init];
[self presentModalViewController:secondViewController animated:YES];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [mytableinfo count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
// Configure the cell...
cell.textLabel.text =[mytableinfo objectAtIndex:indexPath.row];
NSLog(@"Data was written to table");
return cell;
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
mytableinfo = [[NSMutableArray alloc]init];
mytableview = [[UITableView alloc]init];
[mytableinfo addObject:@"Hello world1"];
[self.mytableview reloadData];
}
- (void)viewDidLoad
{
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addShift:)];
self.navigationItem.rightBarButtonItem = addButton;
[super viewDidLoad];
UIBarButtonItem *refresh = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(reloadTableData:)];
self.navigationItem.leftBarButtonItem = refresh;
mytableinfo = [[NSMutableArray alloc]init];
[mytableinfo addObject:@"First Shift"];
[self.mytableview reloadData];
// Do any additional setup after loading the view from its nib.
}

第二 View Controller .m
#import "SecondViewController.h"
#import "FirstViewController.h"

-(IBAction)saveShift:(id)sender{
FirstViewController *firstViewController = [[FirstViewController alloc]init];
firstViewController.mytableinfo = [[NSMutableArray alloc]init];
[firstViewController.mytableinfo addObject:@"SecondViwController Dismissed"];
NSLog(@"%@",[firstViewController.mytableinfo objectAtIndex:0]);
[self dismissModalViewControllerAnimated:YES];
}

所以当 saveShift被称为我应该被带回主视图 - 我这样做 - 然后重新填充我的 tableview .就好像我摆脱了一个小问题却跳入了另一个问题——感觉不是很好!
干杯,
山姆

最佳答案

在您的 saveShift:(id)sender方法你有很多代码。
只需执行以下操作:

[self.myTableInfo addObject:@"SecondViewConroller dimissed"];
[self dismissModalViewControllerAnimated:YES];

在该代码中,我假设您已经在 secondView 中传递了对 NSMutableArray 的引用。

我记得您来自 NSMutableArray 的帖子,并且您正在展示来自 FirstView 的 SecondView(如果我的内存很好的话)。
但是在您的代码中,在 saveShift在第二个 View 中,您目前正在创建 brand new FirstView,这与您来自哪里不同。因此,当您关闭模态视图时,您的“全新”firstView 会丢失并且您会回到原来的 firstView。最后一个还没有听说过你的对象。

你好山姆,
去看看我在你的 NSMutableArray 线程中给你的最后一个答案,那里几乎有你需要解决的一切。

好的,这里有更多的快速修复,以便可以工作
//  SecondViewController.m
#import "SecondViewController.h"
#import "FirstViewController.h"
@implementation SecondViewController
@synthesize dateformatter,mydatepicker,startingTime;
// HERE Add a new property that match this and a variable in the .h
@synthesize array4Test;
// HERE make an init that will make this UIViewController know about your NSMutableArray
- (id)initWithMutableArray:aMutableArray
{
self = [self initWithNibName:nil bundle:nil];
if (self)
{
self.array4Test = aMutableArray;
}
return self;
}
- (void)dealloc
{
// HERE clean up
self.array4Test = nil;
[super dealloc];
}
-(IBAction)saveShift:(id)sender{
// HERE remove this code
//FirstViewController *firstViewController = [[FirstViewController alloc]init];
//[firstViewController.mytableinfo addObject:@"Hello world"];
//NSLog(@"%@",[firstViewController.mytableinfo objectAtIndex:0]);
// HERE add an object to the mutableArray that is store in your firstViewController, the one you've passed the reference in
[self.array4Test addObject:@"This is a String"];
[self dismissModalViewControllerAnimated:YES];
}

我想我刚刚在这个中添加了一个 NSLog() 。
//  FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation FirstViewController
@synthesize mytableinfo,mytableview;
@synthesize goneToOtherView;
-(IBAction)addShift:(id)sender{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithMutableArray:self.mytableinfo];
[self presentModalViewController:secondViewController animated:YES];
}
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.mytableview reloadData];
NSString *aString = [mytableinfo lastObject];
if (aString)
{
NSLog(@"This just came back from the second View\n%@", aString);
}
}

关于ios - 未调用 cellForRowAtIndexPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8029509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com