为什么[self.tableView reloadData]不起作用?

mam*_*ezo 0 iphone objective-c uitableview ios

这是cellForRowAtIndexPath:方法的代码.

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

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];

if (cell == nil) { cell = [[[UITableViewCell alloc]
    initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}

NSUInteger row = [indexPath row]; 
cell.textLabel.text = [array objectAtIndex:row]; 
return cell;    
}
Run Code Online (Sandbox Code Playgroud)

当我[self.tableView reloadData];什么都不用的时候?

Dar*_*ust 10

您需要至少实现tableView:numberOfRowsInSection:方法作为UITableViewDataSource协议的一部分,并且tableView:cellForRowAtIndexPath:(您已经这样做了).你也应该实施numberOfSectionsInTableView:.

然后,您需要确保您的类实际用作数据源:

self.tableView.dataSource = self;
Run Code Online (Sandbox Code Playgroud)

这需要通过Interface Builder或awakeFromNib其他方法完成viewDidLoad:.