簡體   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