简体   繁体   English

ReloadData(UITableView)不起作用

[英]ReloadData (UITableView) doesn't work

I got a big problem. 我有一个大问题。

[self.tableView reloadData];

Doesn't work, and I don't understand why. 不起作用,我也不明白为什么。

[[self tableView] reloadData];

Doesn't work too. 也不行。

Here is my code: 这是我的代码:

.h 。H

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

.m .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;

In the loadPlist method, I'm writing in a .plist file. loadPlist方法中,我正在编写.plist文件。 This part work fine. 这部分工作正常。

Once all is write in the .plist file : 将所有内容写入.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];
}

If I touch btnRight , I can see in the log " Test reloadTheTB ". 如果我触摸btnRight ,则可以在日志“ Test reloadTheTB ”中看到。

Here is tableView:cellForRowAtIndexPath: 这是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;
}

UPDATE: 更新:

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

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

Help me please... 请帮帮我...

Actually I'm wondering this how you know that reloadData is not called? 实际上,我想知道您如何知道不调用reloadData?

Try to put some log in cellForRowAtIndexPath? 尝试在cellForRowAtIndexPath中放置一些日志? to see if is called or not when you click your button? 在单击按钮时查看是否被调用? and also in do this : 并且这样做:

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

} }

Is the content of your dictionary what you expect? 字典的内容是您期望的吗? or is not changed after loadPlist? 还是在loadPlist之后没有更改?

Probably the call to reloadData is not executed on the main thread and you can update the UI only on the main thread. 可能不会在主线程上执行对reloadData的调用,并且您只能在主线程上更新UI。 Try to execute your method on the main thread doing something like this : 尝试在主线程上执行您的方法,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM