2013-06-29 57 views
0

我試圖從模式視圖控制器添加一個單元格到我的表視圖控制器。我的數據源數組有多個屬性(名稱,時間間隔)。現在我在我的模式視圖控制器中有一個委託/協議,將數據發送到表視圖控制器。但是,由於某種原因,我可以將數據添加到數組中,但我無法將單元格添加到具有該數據的tableview中。這裏是我的代碼:不能添加細胞到UITableView從莫塔爾VC到表VC(複雜)

ToDoTableViewController.h(TableViewController)

@interface ToDoTableViewController : UITableViewController <UITableViewDataSource, Properties2ViewControllerDelegate> 
{ 
IBOutlet UIView *headerView; 
} 
@property (strong, nonatomic) NSMutableArray *taskArray; 
-(UIView *)headerView; 
-(IBAction)addCell:(id)sender; 

ToDoTableViewController.m(TableViewController)

-(void) viewDidLoad{ 
    [[self tableView] setDataSource:self]; 
} 
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"]; 
    if (!cell) 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"]; 
[[cell textLabel] setText:[taskArray objectAtIndex:[indexPath row]]]; 
    return cell; 
} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [taskArray count]; 
} 
-(IBAction)addCell:(id)sender{ 
    Properties2ViewController *pvc = [[Properties2ViewController alloc]init]; 
    [pvc setDelegate:self]; 
    [self presentViewController:pvc animated:YES completion:NULL]; 
} 
-(UIView *)headerView{ 
    if (!headerView){ 
     [[NSBundle mainBundle] loadNibNamed:@"HeaderView" owner:self options:nil];   
    } 
    return headerView; 
} 
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ 
    return [self headerView]; 
} 
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ 
    return [[self headerView] bounds].size.height; 
} 
-(void)viewWillAppear:(BOOL)animated{ 
    [super viewWillAppear:animated]; 
    [[self tableView] reloadData]; 
    } 
-(void)properties2ViewControllerDidEnterPropertiesSuccesfully:(Tasks *)t{ 
    [taskArray addObject:t]; 
} 

Properties2ViewController.h(ModalViewController)

@class Tasks; 
@protocol Properties2ViewControllerDelegate; 

@interface Properties2ViewController : UIViewController <UITextFieldDelegate>{ 
__weak IBOutlet UITextField *taskName; 
__weak IBOutlet UIButton *doneButton; 
    __weak IBOutlet UIDatePicker *datePicker; 
    __weak IBOutlet UILabel *label1; 
    __weak IBOutlet UILabel *label2; 
} 
@property (strong, nonatomic) Tasks *testTask; 
@property (weak, nonatomic) id <Properties2ViewControllerDelegate> delegate; 
-(IBAction)dismiss:(id)sender; 
-(IBAction)cancel:(id)sender; 
@end 

@protocol Properties2ViewControllerDelegate <NSObject> 

@optional 
-(void)properties2ViewControllerDidEnterPropertiesSuccesfully:(Tasks *)t; 
@end 

Properties2ViewController。 m(ModalViewController)

-(IBAction)dismiss:(id)sender{ 
    testTask = [[Tasks alloc]initWith:[taskName text] :[datePicker countDownDuration] :[NSDate date]]; 
    if ([self.delegate respondsToSelector:@selector (properties2ViewControllerDidEnterPropertiesSuccesfully:)]){ 
     [self.delegate properties2ViewControllerDidEnterPropertiesSuccesfully:testTask]; 
    } 
    [self dismissViewControllerAnimated:YES completion:NULL]; 
} 
-(void)viewWillDisappear:(BOOL)animated{ 
    [super viewWillDisappear:animated]; 
} 
-(BOOL) textFieldShouldReturn:(UITextField *)aTextField{ 
    if (aTextField.tag == 1){ 
     [taskName resignFirstResponder]; 
    } 
    return YES; 
} 
-(void)viewDidDisappear:(BOOL)animated{ 
    [super viewDidDisappear:animated]; 
} 
-(IBAction)cancel:(id)sender{ 
    [self dismissViewControllerAnimated:YES completion:NULL]; 
} 
@end 

下面是任務類的屬性...如果它有助於在所有...

@interface Tasks : NSObject 
@property (strong, nonatomic) NSString *taskName; 
@property NSTimeInterval timeInterval; 
@property NSDate *dateCreated; 

-(id)initWith:(NSString *)tskNme :(NSTimeInterval)timeInt :(NSDate *)dateCreat; 
@end 

---編輯----- 所以我試圖把這個在我properties2ViewControllerDidEnterPropertiesSuccesfully委託方法,但仍然沒有被創建的電池...:

-(void)properties2ViewControllerDidEnterPropertiesSuccesfully:(Tasks *)t{ 
    [taskArray addObject:t]; 
    [self.tableView reloadData]; 
    int lastRow = [[self tableView] numberOfRowsInSection:0]; 
    NSIndexPath *ip = [NSIndexPath indexPathForItem:lastRow inSection:0]; 
    [self.tableView cellForRowAtIndexPath:ip]; 
} 

另外,如果我切換

[self.tableView cellForRowAtIndexPath:ip]; 

[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:ip] withRowAnimation:UITableViewRowAnimationTop]; 

然後拋出一個異常(終止應用程序由於未捕獲的異常「NSInternalInconsistencyException」,理由是:「試圖插入一行0到段0,但只有0在第0行之後更新「)

+0

Your Properties2ViewController.h與你的ToDoTableViewController.h相同。也許發佈Properties2ViewController的正確代碼,以便我們可以檢查? – ophychius

+0

omg我怎麼沒有注意到...固定 – EvilAegis

回答

0

你在結尾處得到的錯誤提示,進入從不它進入陣列。你可以在調用[taskArray addObject:t]之後檢查你的Array的長度; ?另外,檢查對象的值(t),看看是否真正傳入了testTask。

看來你實際上並沒有創建taskArray,只是聲明它。嘗試在你的ToDoTableViewControllers中加入這個viewDidLoad

taskArray = [[NSMutableArray alloc] init]; 
+0

嗯..你是對的。我添加了一個t對象後,檢查了taskArray,並且計數仍然爲0.但是,taskName值和timeInterval值已成功傳遞。 – EvilAegis

+0

@ user2533646我根據您的反饋添加了一個可能的解決方案 – ophychius

+0

OMG THAT WORKED。是!!你令人驚歎。我太高興了!!!!!!!非常感謝你,我已經在這裏工作了8個小時! – EvilAegis

1

你可能想嘗試[的tableView reloadData]在

- properties2ViewControllerDidEnterPropertiesSuccesfully 
+0

我只是試過,不幸的是它沒有添加任何單元:( – EvilAegis

+0

後屬性2ViewControllerDidEnterPropertiesSuccesfully調用[tableView reloadData],是tableView:cellForRowAtIndexPath調用? – rocky

+0

我不這麼認爲。 indexpath是什麼? – EvilAegis