【问题标题】:UITableView works in ios6 but not in ios 5.1 (array count trouble)UITableView 适用于 ios6 但不适用于 ios 5.1(数组计数问题)
【发布时间】: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 模拟器中逐字验证了您的代码(除了一个例外:我必须在 string1 ivar 声明的末尾添加一个分号才能使其编译)。我建议从 5.1 模拟器中删除您的应用程序,删除任何派生数据并进行干净的构建。如果您将array1.count 更改为[array1 count],则可获得奖励积分(计数不是属性):)
  • 不幸的是,当我(重新)安装我的应用程序时,5.1 模拟器很干净。相反,使用 [table reloadData] 它也适用于 return array1.count!顺便说一句,感谢您记住了我获得奖励积分的方式! ;=)

标签: xcode uitableview row


【解决方案1】:

填充数组1后做

[self.tableView reloadData];

因为我认为加载 tableview 时数据不可用。这在 iOS 5 和 6 之间可能会有所不同。

【讨论】:

  • 太棒了,Roland,我简单地添加了 [table reloadData],它在 iOS 5.1 中也能正常工作……但它是 iOS 5 和 6 之间“众所周知”的区别吗?它在开发人员文档中吗?谢谢!
  • 嗨,Huxley,我不确定为什么会出现这种差异,它可能与渲染和显示视图有关。
  • 奇怪 - 在 iOS 6 和 5.1 中,tableview 的加载方式没有记录的差异,并且示例代码在具有两个版本的操作系统的设备和 sims 上加载数据对我来说都很好。 @Huxley:您确定没有其他逻辑干扰 viewDidLoad/UITableViewDataSource 消息序列吗?
  • 可能是因为我正在使用情节提要,我需要在 UIViewController 类中编写此代码,该类使用“Relationship Segue”链接到“Initial View Controller”(我的是标签栏控制的应用程序,而不是单视图应用程序):此表在我们第一次调用此视图时加载,并且该类还有方法 -(void)awakeFromNib 和 -(void)viewWillAppear:(BOOL)animated,而不仅仅是 -(void)viewDidLoad.. .
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-07
  • 1970-01-01
  • 2013-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多