简体   繁体   English

将表分为iphone应用程序的自定义部分(xcode 4.2)

[英]Breaking a table into custom sections for iphone application (xcode 4.2)

I would like to break my table view into sections. 我想将表格视图分为几个部分。 I would like to see an example, of a table view broken into 3 sections, and then me being able to choose the index where the sections begin. 我想看一个将表视图分为3个部分的示例,然后我可以选择这些部分开始处的索引。

So if I have an array of objects, and they populate a table view. 因此,如果我有一个对象数组,并且它们填充了表格视图。 I would like to choose the titles of the sections and where the sections begin (so for row 1-13 would be section 1, 13-30 would be section 2, etc...). 我想选择这些部分的标题以及这些部分的开头(因此,第1-13行将是第1部分,而13-30将是第2部分,以此类推...)

I have this so far: 到目前为止,我有:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    if (ingredientListChoice == 1) { 
        return 3;

    }

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{
    if (ingredientListChoice == 1) {
    return @"Section Title";
    }
}

Please let me know if you can show me an example of what I am getting at. 请让我知道是否可以向我展示我所得到的例子。 Thank you. 谢谢。

Here's a rough way to do it. 这是一个粗略的方法。 Basically you'll need to return the correct size of each section from tableView:numberOfRowsInSection: and then set the correct content in your table cell by adding an offset to the row index position when pulling the content from your data array in tableView:cellForRowAtIndexPath: . 基本上,您需要从tableView:numberOfRowsInSection:返回每个部分的正确大小,然后在从tableView:cellForRowAtIndexPath:数据数组中提取内容时,通过向行索引位置添加偏移量来设置表单元格中的正确内容tableView:cellForRowAtIndexPath:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    switch (section) {
        case 0: return 13; break;
        case 1: return 17; break;

        etc...
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    int offset = 0;
    switch (section) {
        case 0: offset=0; break;
        case 1: offset=13; break;
        case 2: offset=30; break;

        etc...
    }

    int arrayRow = indexPath.row + offset;
    cell.textLabel.text = [myArray objectAtIndex:arrayRow];

    return cell;
}

A cleaner way might be to store the sizes of your sections in an array you store as a property (which you would perhaps set in viewDidLoad ) and then numberOfRowsInSection and cellForRowAtIndexPath could read the necessary value(s) from that array, so that if in the future you needed to change the sizes of your sections you'd only have to update one place. 一种更干净的方法可能是将节的大小存储在作为属性存储的数组中(您可能会在viewDidLoad设置),然后numberOfRowsInSection和cellForRowAtIndexPath可以从该数组读取必要的值,以便在将来您需要更改部分的大小,只需更新一个地方即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM