简体   繁体   中英

tableView reloadData doesn't work

For some reason, [self.tableView reloadData]; just wont work. In my .h file I have:

@interface SearchOptions : UIViewController <UINavigationBarDelegate, UITableViewDelegate, UITableViewDataSource> 
{
    IBOutlet UITableView *myTableView;
     .....
}

I've tried:

[self.tableView reloadData];

and

[self.myTableView reloadData];

Any ideas what might be happening?

Are you sure it's non- nil ? That is, that your outlet is properly connected?

Is the property tableView synthesized to the ivar myTableView ? (You don't include that code in your question.)

Check that you've correctly bound your outlet in Interface Builder (check myTableView is not nil). If that's not it, are you getting the initial data in your table view? If not, then it probably means you haven't set your SearchOptions instance as the data source for your table—check this in Interface Builder and stick some break points on the data source methods.

I solved it! The problem was that because of other reasons, I wasn't able to reuse my cells and this was my code:

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

NSString *kCustomCellID = [NSString stringWithFormat:@"MyCellID%d", indexPath];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCustomCellID];
if (cell == nil) 
{
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCustomCellID] autorelease];
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;

    if (selectOrDeselectInteger == 1) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    }
    if (selectOrDeselectInteger == 2) {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
}

I just had to add this afterwards:

    if (cell != nil) 
{
    if (selectOrDeselectInteger == 1) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark; 
    }
    if (selectOrDeselectInteger == 2) {
        cell.accessoryType = UITableViewCellAccessoryNone;          
    }
}

Thanks everyone for the help. Sorry this code wasn't in the question.

您的插座未在IB中连接。

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