iPhone:UITableView未刷新

iPhone:UITableView未刷新,iphone,uitableview,refresh,Iphone,Uitableview,Refresh,我读过很多关于iTableViews在iPhone上不刷新的主题,但找不到任何与我的情况相匹配的内容,所以我请求帮助 在扩展UIViewController的类中,我有一个TableView和一个“ElementList”(是NSMutableArray的包装器)用作数据源 一个单独的线程通过'updateList:'方法将新元素添加到数组中。 发生这种情况时,我希望tableView自动刷新,但这不会发生 通过调试我的应用程序,我可以看到从未调用过“cellForRowAtIndexPath”

我读过很多关于iTableViews在iPhone上不刷新的主题,但找不到任何与我的情况相匹配的内容,所以我请求帮助

在扩展UIViewController的类中,我有一个TableView和一个“ElementList”(是NSMutableArray的包装器)用作数据源

一个单独的线程通过'updateList:'方法将新元素添加到数组中。 发生这种情况时,我希望tableView自动刷新,但这不会发生

通过调试我的应用程序,我可以看到从未调用过“cellForRowAtIndexPath”,我也不知道为什么

我尝试添加一个观察器,它调用“reloadTableView”方法(实际上调用了该方法),但tableView没有更新

这是我的代码:

#import <UIKit/UIKit.h>

@interface ListViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
{
    UITableView *tableView;
    ElementList *elementList;   // Wrapper for NSMutableArray
}

// Called by someone else, triggers the whole thing
-(void)updateList:(Element *)element;

// Added out of desperation
-(void)reloadTableView;

@end


@implementation ListViewController

-(void)loadView
{
    // Create the TableView
    tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
    assert(tableView != nil);
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView reloadData];

    self.view = tableView;

    // Added out of desperation 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTableView) name:@"UpdatedListNotification" object:nil];
}

-(void)reloadTableView
{
    // Try anything
    [tableView reloadData];
    [tableView setNeedsLayout];
    [tableView setNeedsDisplay];
    [tableView reloadData];
    [self.view setNeedsLayout];
    [self.view setNeedsDisplay];
}

// Called by someone else, triggers the whole thing
-(void)updateList:(Element *)element
{
    assert(element != nil);
    [elementList addElement:element];
    [element release];

    // Notify the observer, which should update its table view
    [[NSNotificationCenter defaultCenter] postNotificationName:@"UpdatedListNotification" object:self];
}

// TableView Delegate protocol
- (void)tableView:(UITableView *)table didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Element *selected_element = [elementList getElementAtIndex:indexPath.row];
    if (selected_element == nil)
    {
        NSLog(@"ERROR: Selected an invalid element");
        return;
    }
    [table deselectRowAtIndexPath:indexPath animated:YES];

    NSLog(@"Selected %@", selected_element.name);
}

// TableView Data Source protocol
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [elementList count];
}

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath
{       
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    // Set the cell label
    cell.textLabel.text = [[elementList getElementAtIndex:indexPath.row] name];
    cell.textLabel.frame = cell.bounds;
    cell.textLabel.textAlignment = UITextAlignmentLeft;
    return cell;
}

@end
#导入
@接口ListViewController:UIViewController
{
UITableView*表格视图;
ElementList*ElementList;//NSMutableArray的包装器
}
//被其他人呼叫,触发了整个事件
-(void)updateList:(Element*)元素;
//不顾一切
-(void)重新加载TableView;
@结束
@ListViewController的实现
-(void)负荷视图
{
//创建TableView
tableView=[[UITableView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]样式:UITableView样式普通];
断言(tableView!=nil);
tableView.delegate=self;
tableView.dataSource=self;
[tableView重新加载数据];
self.view=tableView;
//不顾一切
[[NSNotificationCenter defaultCenter]添加观察者:自选择器:@selector(reloadTableView)名称:@“UpdatedListNotification”对象:nil];
}
-(void)重新加载TableView
{
//试试看
[tableView重新加载数据];
[tableView setNeedsLayout];
[表视图设置需要显示];
[tableView重新加载数据];
[self.view setNeedsLayout];
[self.view setNeedsDisplay];
}
//被其他人呼叫,触发了整个事件
-(void)updateList:(元素*)元素
{
断言(元素!=nil);
[elementList addElement:element];
[元素释放];
//通知观察者,观察者应更新其表视图
[[NSNotificationCenter defaultCenter]postNotificationName:@“UpdatedListNotification”对象:self];
}
//TableView委托协议
-(void)tableView:(UITableView*)表didSelectRowatineXpath:(NSIndexPath*)indexPath
{
Element*selected_Element=[elementList getElementAtIndex:indexath.row];
if(所选元素==nil)
{
NSLog(@“错误:选择了无效元素”);
返回;
}
[表取消行索引路径:indexPath:是];
NSLog(@“Selected%@”,Selected_element.name);
}
//TableView数据源协议
-(NSInteger)表格视图中的节数:(UITableView*)表格视图
{
返回1;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
{
返回[元素列表计数];
}
-(UITableViewCell*)tableView:(UITableView*)table cellForRowAtIndexPath:(NSIndexPath*)indexPath
{       
静态NSString*CellIdentifier=@“Cell”;
UITableViewCell*单元格=[表dequeueReusableCellWithIdentifier:CellIdentifier];
如果(单元格==nil)
{
cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault重用标识符:CellIdentifier]自动释放];
}
//设置单元格标签
cell.textlab.text=[[elementList getElementAtIndex:indexath.row]name];
cell.textlab.frame=cell.bounds;
cell.textLabel.textAlignment=UITextAlignmentLeft;
返回单元;
}
@结束

非常感谢您的帮助。

通知与调用者在同一线程中执行。更新UI实际上应该在主线程中完成,因此您应该在主线程上调用-reloadData

-(void)updateList:(Element *)element
{
    assert(element != nil);
    [elementList addElement:element];

    [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil];
}

还请注意,您不应该释放您不拥有的对象。所以不要在-updateList方法中调用[element release]。该版本应该由函数的调用者调用。

这对我来说不太合适,但非常接近。我采用的方法是——

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
你也可以

[self performSelectorOnMainThread:@selector(reloadTable) withObject:nil waitUntilDone:YES];
这样您就可以在ViewController上实现一个方法来完成所有需要的UI工作

-(void)reloadTable
{
   [self.tableView reloadData];
}

我很高兴能帮上忙。当你在做的时候,考虑一下答案,点击左边的大号“V”: