【发布时间】:2012-12-05 20:24:01
【问题描述】:
这是我填充 UITableView 的代码,非常非常简单:
文件.h
@interface ViewController: UIViewController {
IBOutlet UITableView *table;
NSMutableArray *array1;
NSString *string1
}
文件.m
- (void)viewDidLoad
{
[super viewDidLoad];
array1 = [[NSMutableArray alloc] initWithObjects: @"1", @"2", @"3", @"4", nil];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return array1.count; // <------- here there is the problem with iOS 5.1, also [array1 count]
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 20;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.detailTextLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.text = [NSString stringWithFormat:@"???? %@",[array1 objectAtIndex:indexPath.row]];
cell.textLabel.font = [UIFont systemFontOfSize:13];
cell.textLabel.textColor = [UIColor blackColor];
return cell;
}
好吧,在 iOS 6(模拟器和真实设备)中没关系,UITableView 加载并显示 array1 对象的所有行;在 iOS 5.1(模拟器和真实设备)中它不会崩溃,但 UITableView 是空的,没有显示行(附有页眉和页脚),我注意到这可能是 numberOfRowsInSection: array1.count 方法的问题,我认为iOS 5.1 它不能识别正确的行数。请问,编写与 iOS 5.1 和 iOS6 兼容的代码的最佳方法是什么?
【问题讨论】:
-
您不需要致电
reloadData。确实:我已经在 iOS 6.0 和 5.1 模拟器中逐字验证了您的代码(除了一个例外:我必须在string1ivar 声明的末尾添加一个分号才能使其编译)。我建议从 5.1 模拟器中删除您的应用程序,删除任何派生数据并进行干净的构建。如果您将array1.count更改为[array1 count],则可获得奖励积分(计数不是属性):) -
不幸的是,当我(重新)安装我的应用程序时,5.1 模拟器很干净。相反,使用 [table reloadData] 它也适用于 return array1.count!顺便说一句,感谢您记住了我获得奖励积分的方式! ;=)
标签: xcode uitableview row