我有以下功能:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"stateCell" forIndexPath:indexPath];
NSString *state = self.stateArray[indexPath.row];
cell.textLabel.text = state;
return cell;
}
状态字符串全部来自此NSArray
:
self.stateArray = [NSArray arrayWithObjects:@"Alabama", @"Alaska", @"Arizona", @"Arkansas", @"California", @"Colorado", @"Connecticut", @"Delaware", @"Florida", @"Georgia", @"Hawaii", @"Idaho", @"Illinois", @"Indiana", @"Iowa", @"Kansas", @"Kentucky", @"Louisiana", @"Maine", @"Maryland", @"Massachusetts", @"Michigan", @"Minnesota", @"Mississippi", @"Missouri", @"Montana", @"Nebraska", @"Nevada", @"New Hampshire", @"New Jersey", @"New Mexico", @"New York", @"North Carolina", @"North Dakota", @"Ohio", @"Oklahoma", @"Oregon", @"Pennsylvania", @"Rhode Island", @"South Carolina", @"South Dakota", @"Tennessee", @"Texas", @"Utah", @"Vermont", @"Virginia", @"Washington", @"West Virginia", @"Wisconsin", @"Wyoming", nil];
我UITableViewController
中的所有文字标签都显示为“Alabama”。
我做错了什么?
答案 0 :(得分:0)
我不小心将部分功能设置为:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [self.stateArray count];
}
应该是
return 1;
答案 1 :(得分:0)
每当您将单元格返回到UITableView数据源时,您必须检查单元格是否等于nill:
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"stateCell" forIndexPath:indexPath];
有时这个函数会返回nill,因此你必须检查单元格是否等于nill,如果它需要分配一个新的单元格,那么继续你的代码:
if (!cell)
cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: @"stateCell"];
在创建单元格变量后,会立即执行。
我希望这可以解决问题:)