2012-12-04 71 views
1

我已經成功地設置了一個UITableView,它從一個數組中抽取它的單元格標籤。我正在嘗試的是將一個對象添加到該數組並重新加載UITableView,以便它將顯示該新對象並創建一個新的細胞不幸的是,我沒有運氣將對象添加到NSMutableArray,嘗試在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 

和我的整個.M:

@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 
+0

記錄並檢查數組數,你可以知道天氣的對象是否被添加。 – vamsi575kg

+0

哪個視圖你正在解僱dismissViewControllerAnimated? taskList和mainTableView被聲明在哪裏? –

+0

其中mainTableView類在哪? –

回答

1

請確保你已經完成了任務列表數組的初始化。 像'tasksList = [[NSMutableArray alloc] init]'。

+0

我剛剛編輯了我的文章並添加了我的所有代碼,謝謝! – ranjha

0

我想,你是硬編碼numberOfRowsInSection請,這將是 -

   - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section { 
        return [tasksList count]; 
       } 
+0

我剛剛編輯我的帖子,並添加了我的所有代碼,謝謝! – ranjha

+0

我也有我的代碼。 – ranjha

1

你嘗試

[self dismissViewControllerAnimated:YES completion:^{ 
    [mainTableView reloadData]; 
}]; 

而且,把斷點和對reloadData號召,TableView中代表檢查並調用DataSource方法。