繁体   English   中英

ReloadData(UITableView)不起作用

[英]ReloadData (UITableView) doesn't work

我有一个大问题。

[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];
}

请帮帮我...

实际上,我想知道您如何知道不调用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]
}

暂无
暂无

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

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