无法将模块从Modal VC添加到UITableView到表VC(复杂)

时间:2013-06-29 00:11:05

标签: iphone ios objective-c tableview modalviewcontroller

我正在尝试从模态视图控制器向我的表视图控制器添加一个单元格。我的数据源数组有多个属性(名称,时间间隔)。现在我在我的模态视图控制器中有一个委托/协议,它将数据发送到表视图控制器。但是,出于某种原因,我可以将数据添加到数组中,但我无法使用该数据向表视图添加单元格。这是我的代码:

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行')

2 个答案:

答案 0 :(得分:1)

您可能想在

中尝试[tableView reloadData]
- properties2ViewControllerDidEnterPropertiesSuccesfully

答案 1 :(得分:0)

最后得到的错误表明该条目从未进入数组。调用[taskArray addObject:t]后,可以检查数组的长度吗? ?另外,检查对象的值(t)并查看是否实际传入了正确的testTask。

看来你实际上并没有创建taskArray,只是声明它。尝试在ToDoTableViewControllers viewDidLoad

中添加它
taskArray = [[NSMutableArray alloc] init];