UITableView问题 - 更新表值

时间:2011-05-09 10:46:31

标签: xcode ios uitableview

我是新手。我为UITable编写了一些代码,但在将值添加到数组后,我无法更新表值。我正在使用tableview子类。代码如下。检查最后一个功能。现在我该如何更新我的表值?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [_phones count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

       HLPFoundPhones *p = (HLPFoundPhones *)[_phones objectAtIndex:indexPath.row];
       NSString *subtitle = [NSString stringWithFormat:@"Found (%1.2f,%1.2f) on %@" ,[p loc].x, [p loc].y , [p foundDate]];
       cell.textLabel.text = [p name];
       cell.detailTextLabel.text = subtitle;

       return cell;
}


- (void)insertNewObject
{
    HLPPhonesAdd *view = [[HLPPhonesAdd alloc]initWithNibName:@"HLPPhonesAdd" bundle:nil];
    [self.navigationController pushViewController:view animated:YES];
    [view release];

   }
- (void)updateTable:(CGPoint)loc name:(NSString *)_name{
    HLPFoundPhones *a = [[HLPFoundPhones alloc]initWithLoc:loc name:_name];
    [_phones addObject:a];
    [phones reloadData];

}

4 个答案:

答案 0 :(得分:0)

致电

[yourTableView reloadData];
你的updateTable函数中的

..

- (void)updateTable:(CGPoint)loc name:(NSString *)_name{
    HLPFoundPhones *a = [[HLPFoundPhones alloc]initWithLoc:loc name:_name];
    [_phones addObject:a];

   //give your tableView object instead of yourTableView
   [yourTableView reloadData];

}

答案 1 :(得分:0)

检查_phone计数值并在函数中使用

- (void)updateTable:(CGPoint)loc name:(NSString *)_name
{
    HLPFoundPhones *a = [[HLPFoundPhones alloc]initWithLoc:loc name:_name];
    [_phones addObject:a];
    [table_name reloaddata];
}

答案 2 :(得分:0)

确认已分配数组_phones

如果在线程内调用[tableview reloadData],则会出现问题。 因此,请确认从主线程调用reloadData。

答案 3 :(得分:0)

首先,您的小区标识符对于所有小区看起来都是相同的,这可能会使iOS混淆哪个小区需要出列。因此,请将您的单元格标识符保持唯一,如下所示:

NSString *cellIdentifier = [NSString stringWithFormat:@"Cell-%d", indexPath.row];

然后,在首次显示表视图后,更新数据源并调用:

[tableView reloadData];
相关问题