我試圖從模式視圖控制器添加一個單元格到我的表視圖控制器。我的數據源數組有多個屬性(名稱,時間間隔)。現在我在我的模式視圖控制器中有一個委託/協議,將數據發送到表視圖控制器。但是,由於某種原因,我可以將數據添加到數組中,但我無法將單元格添加到具有該數據的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行之後更新「)
Your Properties2ViewController.h與你的ToDoTableViewController.h相同。也許發佈Properties2ViewController的正確代碼,以便我們可以檢查? – ophychius
omg我怎麼沒有注意到...固定 – EvilAegis