我正在尝试从模态视图控制器向我的表格视图控制器添加一个单元格。我的数据源数组有多个属性(名称、时间间隔)。现在我的模态视图控制器中有一个委托/协议,它将数据发送到表视图控制器。但是,由于某种原因,我可以将数据添加到数组中,但我无法使用该数据将单元格添加到 tableview 中。这是我的代码:
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
这是 Task 类的属性……如果它有帮助的话……
@interface Tasks : NSObject
@property (strong, nonatomic) NSString *taskName;
@property NSTimeInterval timeInterval;
@property NSDate *dateCreated;
-(id)initWith:(NSString *)tskNme :(NSTimeInterval)timeInt :(NSDate *)dateCreat;
@end
---EDIT----- 所以我试着把它放在我的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行')