Iphone 正在将对象添加到NSMutableArray,尝试在UITableView上重新加载数据,新对象未显示:(
我已成功设置了一个Iphone 正在将对象添加到NSMutableArray,尝试在UITableView上重新加载数据,新对象未显示:(,iphone,objective-c,Iphone,Objective C,我已成功设置了一个UITableView,它从数组中提取单元格标签。我正在尝试的是向该数组中添加一个对象并重新加载UITableView,以便它显示该新对象并创建一个新单元格。不幸的是,我没有运气 我知道这可能是我在代码中做的非常愚蠢的事情,但是有人能告诉我哪里出了错吗?以下是我目前使用的: 这是我的全部。h: @interface crastinatrViewController : UIViewController <UITableViewDataSource, UITableView
UITableView
,它从数组中提取单元格标签。我正在尝试的是向该数组中添加一个对象并重新加载UITableView,以便它显示该新对象并创建一个新单元格。不幸的是,我没有运气
我知道这可能是我在代码中做的非常愚蠢的事情,但是有人能告诉我哪里出了错吗?以下是我目前使用的:
这是我的全部。h:
@interface crastinatrViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
NSMutableArray *tasksList;
UIView *addtaskview;
}
@property (strong, nonatomic) IBOutlet UITableView *mainTableView;
@property (nonatomic, retain) NSMutableArray *tasksList;
@property (strong, nonatomic) IBOutlet UITextField *taskName;
- (IBAction)addTask:(id)sender;
- (IBAction)startaddTask:(id)sender;
@end
@interface crastinatrViewController ()
@end
@implementation crastinatrViewController
@synthesize mainTableView;
@synthesize tasksList;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
tasksList = [[NSMutableArray alloc] initWithObjects:nil];
self.mainTableView.delegate = self;
self.mainTableView.dataSource = self;
[self.taskName becomeFirstResponder];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSInteger rows = [[self tasksList] count];
NSLog(@"rows is: %d", rows);
return rows;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *contentForThisRow = [[self tasksList] objectAtIndex:[indexPath row]];
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
// Do anything that should be the same on EACH cell here. Fonts, colors, etc.
}
// Do anything that COULD be different on each cell here. Text, images, etc.
[[cell textLabel] setText:contentForThisRow];
return cell;}
-(IBAction)startaddTask:(id)sender {
addtaskview = [self.storyboard instantiateViewControllerWithIdentifier:@"addTaskView"];
[self presentViewController:addtaskview animated:YES completion:nil];
}
- (IBAction)addTask:(id)sender {
[tasksList addObject:self.taskName.text];
[self dismissViewControllerAnimated:YES completion:nil];
[mainTableView reloadData];
}
@end
请确保已完成任务列表数组的初始化。
比如“tasksList=[[NSMutableArray alloc]init]”。我想,您是硬编码的行数。请检查,它将是-
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
return [tasksList count];
}
你试过了吗
[self dismissViewControllerAnimated:YES completion:^{
[mainTableView reloadData];
}];
另外,放置断点并检查调用
reloadData
时是否调用了TableView委托和DataSource方法。记录并检查数组计数,您可以知道对象是否已添加。您正在解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除解除ableView??您的代码中是否有tasksList=[[NSMutableArray alloc]init]
代码?共享tableview实现的代码,包括数据源和委托。如果没有这些,将很难提供帮助。