【问题标题】:Can't add cell to UITableView from Modal VC to Table VC (complicated)无法将单元格从 Modal VC 添加到 UITableView 到 Table VC(复杂)
【发布时间】:2013-06-29 00:11:05
【问题描述】:

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

---编辑----- 所以我试着把它放在我的 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];

然后抛出异常(Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 0 into section 0, but there are only 0 rows in section 0 after the update')

【问题讨论】:

  • 您的 Properties2ViewController.h 与您的 ToDoTableViewController.h 相同。也许发布 Properties2ViewController 的正确代码以便我们检查?
  • 天哪,我怎么没注意到...已修复

标签: iphone ios objective-c tableview modalviewcontroller


【解决方案1】:

您可能想尝试 [tableView reloadData] in

- properties2ViewControllerDidEnterPropertiesSuccesfully

【讨论】:

  • 我刚试过,不幸的是它没有添加任何单元格:(
  • properties2ViewControllerDidEnterProperties成功调用[tableView reloadData]后,是否调用tableView:cellForRowAtIndexPath?
  • 我不这么认为。索引路径是什么?
  • 我刚试过这个:'-(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]; }'
  • 好吧,这个评论搞砸了格式,所以我用我所做的编辑了主要帖子
【解决方案2】:

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

看来您实际上并没有创建taskArray,只是声明它。尝试在您的 ToDoTableViewControllers viewDidLoad 中添加它

taskArray = [[NSMutableArray alloc] init];

【讨论】:

  • 嗯..你是对的。添加t对象后查看taskArray,count还是0,但是taskName值和timeInterval值都传成功了。
  • @user2533646 我根据您的反馈添加了一个可能的解决方案
  • 天哪,成功了。是的!!你太棒了。我很高兴!!!!!!!非常感谢我已经为此工作了 8 个小时!!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-17
  • 2015-04-10
相关资源
最近更新 更多