2013-01-10 27 views
-1

我运行这个功能:不能运行在Objective-C函数

- (void)insertRows { 
    [_objects insertObject:[NSDate date] atIndex:0]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
} 

,它必须运行此功能

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath]; 

    NSDate *object = _objects[indexPath.row]; 
    cell.textLabel.text = [object description]; 
    return cell; 
} 

,但此功能无法运行......请帮助如何运行的tableView ...功能

- (IBAction)insertObject { 
    [[MasterViewController alloc] insertRows]; 
} 

- 此代码运行insertRows,我用的断点

检查此
- (IBAction)insertObject:(id)sender { 
    [_objects insertObject:[NSDate date] atIndex:0]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; 
    [self.tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    NSLog(@"log:insert-indexPath: %i",indexPath.row); 
    NSDate *object = _objects[indexPath.row]; 
    NSLog(@"log:insert-object %@",object.description); 
} 

-works与它,为什么?

+0

可以'self.tableview'为零? – Chuck

+0

Chuck,no ...因为如果我通过IBAction(tap button)运行insertRow ... tableview ...作品... – EarthUser

+0

因此可能你没有调用该方法。 –

回答

2

有这个代码的一些问题:

•可能要addObject:_objects,不insertObject:atIndex:

[[MasterViewController alloc] insertRows];没有意义。某处需要有一个init

•不应该使用description方法作为向用户显示某些东西的方法。 description仅用于调试和记录。

•要触发调用tableView:cellForRowAtIndexPath:,您通常会使用reload表。即该方法在更新自身时由表视图调用。

我建议从table view programming guide开始。

+0

中调用这个方法,但是为什么这个方法和它一起工作(见主题) – EarthUser