简体   繁体   中英

UITableView is not reloading

I am making an App that uses a UITableView and UITableViewController . However, when I call [self.tableView reloadData]; in the UITableViewController , nothing happens, ( tableView: cellForRowAtIndexPath: is not called). What should I do?

- (void) UserChangedAmmountAndCurrency: (NSNotification*) notification {
    self.localBank.activeAmmount=((NSNumber*)[notification.userInfo objectForKey:@"newAmmount"]).doubleValue;
    self.localBank.activeCurrency=[self.localBank.worldBank requestCurrencyForLocalBank:((NSString*)[notification.userInfo objectForKey:@"newISO4217Currency"])];
    [self.tableView reloadData];
}

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

    NSNumber *ammount= [self.localBank ammountForElementAtIndex:indexPath.row];
    VIPCurrency* currency=[self.localBank currencyForElementAtIndex:indexPath.row];


    static NSString *CellIdentifier = @"cellWithCurrencyAndAmmount";
    VIPConversionCell* cell=[self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.longCurencyIdentifier.text=currency.longName;
    cell.shortCurrencyIdentifier.text=currency.shortName;
    cell.textfieldForAmmount.text=ammount.stringValue;
    return cell;
}

EDIT: I've done some extra testing. Ive tested that the tableview datasource and delegate are the viewcontroller that I use to call the reload method, which was ok. I've also noticed something else weird. When calling reloadData, neither numberOfSectionsInTableView: nor numberOfRowsInSection: is called. I feel like I'm failing to do a basic thing before calling reloadData, but I don't know what. Could it be that I must somehow specifically set cells as out of date?

EDIT: I've also checked that the method was called in the Main Thread, which was also ok

The idea of checking for nil from dequeReusableCellWithIdentifier call is to create a new cell and return it. Unless you created a cell with the given identifier before and is in reusable pool, that method will return nil . So, as @Verec mentioned, you should check for nil and create the cell yourself and return .

.xib文件和.h文件(UITableViewDataSource, UITableViewDelegate)设置表视图委托

[myTableView reloadData];

You just need to add

UITableViewDataSource,UITableViewDelegate

in .h

example

@interface yourviewcontroller : UIViewController <UITableViewDataSource,UITableViewDelegate>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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