0

我对 iOS 开发很陌生,似乎找不到问题的答案:我有一个包含(除其他外)TableView 的 ViewController。这个 TableView 填充了来自可变数组的项目,我想要做的是创建一个表,如果可变数组只有 2 个条目,则该表将仅由 2 行组成。而且,如果表格有超过(比方说)5 个条目,TableView 应该有一个滚动。

关于如何做到这一点的任何想法?

谢谢!

4

1 回答 1

0

Its pretty much simple. Lets say your array is chuckJokes. Just add the array as a property or member of the class so that you can use it in all methods.

Then populate the array with whatever values you need. Later follow the following methods to populate the tableview according to the array.

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [chuckJokes count];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"chuck"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"chuck"] autorelease];
    }
    cell.text = [chuckJokes objectAtIndex:[indexPath.row]];

    return cell;
}
于 2013-01-27T13:43:55.123 回答