开发者

About sectioning your UITableView

I开发者_JAVA技巧'm working on a tableview, which displays more than one line of text in its cell. I want to have two sections, which I've already done. But is it possible to display another array in the second section, not the same? T

Here's the code I use:

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
            if (section == 0) { 
            return MIN([titles count], [subtitles count]);
            }
            else {
                return 1;
            }
        }


Of course you can do this.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) { 
        return MIN([titles count], [subtitles count]);
    }
    else {
        return MIN([titles2 count], [subtitles2 count]);
    }
}

Just make sure to switch arrays in all UITableViewDataSource and UITableViewDelegate methods.


But I would suggest nested arrays. It makes the code shorter, because when you have more than 10 sections your code will become long and ugly.

And with nested arrays you only have the long ugly code in viewDidLoad or whereever you initialize your array;

titles = [NSArray arrayWithObjects:titles0, titles1, nil];
subtitles = [NSArray arrayWithObjects:subtitles0, subtitles1, nil];

.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return MIN([[titles objectAtIndex:section] count], [[subtitles objectAtIndex:section]count]);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜