iPhone:UITableView 不刷新

标签 iphone uitableview refresh

我读过很多关于 UITableViews 在 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

非常感谢任何帮助,谢谢。

最佳答案

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

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

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

另请注意,您不应释放不属于您的对象。因此,不要在 -updateList 方法中调用 [element release]。释放应该由函数的调用者调用。

关于iPhone:UITableView 不刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2093132/

相关文章:

iphone - 如何确定哪个事件称为imageMoved :withEvent:forControlEvents?

iphone - UIPopoverController 内容 View 大小

ios - 将按钮或 UIView 放在视频顶部

ios - UITableView 在滚动/分页时没有正确处理 contentOffset

ios - UITableView 和模型数组中的单元格

android - cordova-ionic ngCordova ios 或 iPhone 文件读取错误代码 5 ENCODING_ERR

ios - 滚动时重新排序 UITableViewCells

javascript - 如何仅在页面上没有事件时自动刷新 HTML?

javascript - javascript 运行后我的网页刷新

android - 如何在 onSizeChanged 中设置大小和布局?