簡體   English   中英

UITableView reloadData不起作用

[英]UITableView reloadData doesn't work

我已經閱讀了所有類似的問題,並嘗試了所有建議,但還是沒有。 也許有人可以發現我的缺點。

我的視圖控制器由另一個視圖控制器通過兩個按鈕之一啟動。 點擊按鈕會發送NSNotification(帶有附加的數組),並且此視圖控制器會預期此通知,然后調用此方法:

- (void)addContentToArray:(NSNotification *)aNotification {

    array = [NSArray arrayWithArray:[aNotification object]];

    ([array count] == 6) ? (category = YES) : (category = NO);

    [myTableView reloadData];
    NSLog(@"%d", [array count]);
    NSLog(@"%@", myTableView);
}

每次都會調用該方法,我可以從更改數組計數中看到這一點。 此處的通知對象是從以前的視圖控制器傳遞的數組,我將這些對象分配給本地數組屬性-這是我的UITableView源。 因此,我要做的是嘗試重用UITableView來顯示要傳遞的任何數組的元素。 它對於傳遞的第一個數組(以先到者為准)效果很好。

當我點擊第二個按鈕時,新數組成功傳遞(如前所述,我從[array count]的日志中知道這是不同的:不同數組中3個對象與6個對象)。 但是,沒有發生的事情是UITableView不會刷新(盡管我選擇表中的行時傳遞的值來自正確的數組,即使顯示了錯誤的值)。

以下是UITableView數據源方法:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Identifier"];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Identifier"];
    }

    cell.textLabel.text = [[array objectAtIndex:indexPath.row] objectForKey:@"name"];

    if (category) {
        cell.detailTextLabel.text = [[array objectAtIndex:indexPath.row] objectForKey:@"description"];
    }

    return cell;
}

那么,我在做什么錯呢?

其他一些考慮因素可能會有所幫助:

  • NSLog(@“%@”,myTableView); 返回(空),這有點令人擔憂。 myTableView是我的nib文件中的UITableView,該文件已正確連接到視圖控制器,聲明為屬性並合成了
  • 有問題的視圖控制器是PKRevealController的rightViewController ,因此,當反復調用它時,將調用viewWillAppear方法,但不會調用viewDidLoad(盡管正如我已經提到的,每次也會調用addContentToArray:方法)
  • 此外,對於有些熟悉PKRevealController的人-當我嘗試從我的視圖控制器中登錄focusedController時,它表示frontViewController-用來顯示我的視圖控制器的那個-是被聚焦的一個。 那是myTableView為(null)的原因嗎?

我將不勝感激和幫助!

需要更多代碼,該代碼是您在其中創建的,並調用MyviewcontrolleraddContentToArray方法。 我認為您在其中使用了Myviewcontroller對象的發布代碼,請嘗試隱藏該部分。

我設法通過編輯initWithNibName方法解決了該問題(舊行已注釋掉)

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {

    //self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    self = [super initWithNibName:@"PurposeCategoryViewController" bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

顯然,它確實與我的視圖控制器(稱為PurposeCategoryViewController)不是PKRevealController層次結構中的頂部/焦點視圖控制器有關。 因此,我只需要具體指出我的筆尖文件即可。

您在此委托方法中返回的值是:-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView確保它為1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM