【问题标题】:ReloadData (UITableView) doesn't workReloadData (UITableView) 不起作用
【发布时间】:2012-10-30 16:07:06
【问题描述】:

我遇到了一个大问题。

[self.tableView reloadData];

不起作用,我不明白为什么。

[[self tableView] reloadData];

也不行。

这是我的代码:

.h

@interface ArticleViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate> 
{ 
} 
@end

.m

- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.dataSource = self;
self.tableView.delegate = self;
}

btnLeft = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btnLeft"]
                                         style:UIBarButtonItemStylePlain
                                        target:self
                                        action:@selector(loadPlist)];
self.navigationItem.leftBarButtonItem = btnLeft;

loadPlist 方法中,我正在编写一个.plist 文件。这部分工作正常。

一旦全部写入 .plist 文件:

btnRight = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btnRight"]
                                          style:UIBarButtonItemStylePlain
                                         target:self
                                         action:@selector(reloadTheTB)];
self.navigationItem.rightBarButtonItem = btnRight;

- (void)reloadTheTB {
NSLog(@"Test reloadTheTB");

[[self tableView] reloadData];
}

如果我触摸btnRight,我可以在日志中看到“Test reloadTheTB”。

这里是tableView:cellForRowAtIndexPath:

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if(cell == nil)
            cell = [self getCellContentView:CellIdentifier];

        contentDictio = [dict objectAtIndex:indexPath.row];

        UILabel *lblTemp1 = (UILabel *)[cell viewWithTag:1];

        lblTemp1.text = [contentDictio objectForKey:@"title"];

        if(indexPath.row % 2) {
            UIView* myBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
            myBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_grise"]];
            cell.backgroundView = myBackgroundView;
        }
        else {
            UIView* myBackgroundView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
            myBackgroundView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"cell_blanche"]];
            cell.backgroundView = myBackgroundView;
        }
    }

    return cell;
}

更新:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}

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

请帮帮我...

【问题讨论】:

  • 你在哪里创建了tableview????
  • 那么[tableView reloadData]没有反映你正在做的改变是什么
  • 这是一个UITableViewController 所以,我不需要创建tableView
  • 更改的是plist文件中的新闻条目
  • 行和节的委托方法在哪里????

标签: ios uitableview plist reloaddata


【解决方案1】:

其实我很好奇你是怎么知道reloadData没有被调用的?

尝试在 cellForRowAtIndexPath 中输入一些日志?看看当你点击你的按钮时是否被调用?并且这样做:

- (void)reloadTheTB {
   NSLog(@"Test reloadTheTB");
   NSLog(@"%@",dict);
  [[self tableView] reloadData];

}

您的字典内容是您所期望的吗?还是loadPlist后没有变化?

可能 reloadData 的调用没有在主线程上执行,您只能在主线程上更新 UI。尝试在主线程上执行您的方法,执行以下操作:

-(void)ReloadTheTB{   
  [self.tableView performSelectorOnMainThread@selector(reloadData) withObject:nil waitUntilDone:NO]
}

【讨论】:

  • 我所期望的 plist 的内容。如果我在cellForRowAtIndexPath 中执行NSLog(@"%@", dict),我得到了我所有的字典。如果我在- (void)reloadTheTB 中执行NSLog(@"%@", dict);,那么日志中只有( )...我真的不明白发生了什么
  • 目标是了解您在调用 reloadData 后是否看到您放入 cellForRowAtIndexPath 的日志消息。你有没有看到日志信息?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-26
  • 2010-12-13
  • 1970-01-01
相关资源
最近更新 更多