0

これは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 は空で、行は表示されません (ヘッダーとフッターが添付されています)。 iOS 5.1 では、正しい行数が認識されません。iOS 5.1 および iOS6 と互換性のあるコードを記述する最良の方法を教えてください。

4

1 に答える 1

1

array1にデータを入力した後、

[self.tableView reloadData];

テーブルビューが読み込まれるとデータが利用できないと思うので。そして、これはiOS5と6の間で異なる可能性があります。

于 2012-12-05T12:03:12.990 に答える