繁体   English   中英

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

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

我想将表格视图分为几个部分。 我想看一个将表视图分为3个部分的示例,然后我可以选择这些部分开始处的索引。

因此,如果我有一个对象数组,并且它们填充了表格视图。 我想选择这些部分的标题以及这些部分的开头(因此,第1-13行将是第1部分,而13-30将是第2部分,以此类推...)

到目前为止,我有:

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

    if (ingredientListChoice == 1) { 
        return 3;

    }

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

请让我知道是否可以向我展示我所得到的例子。 谢谢。

这是一个粗略的方法。 基本上,您需要从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;
}

一种更干净的方法可能是将节的大小存储在作为属性存储的数组中(您可能会在viewDidLoad设置),然后numberOfRowsInSection和cellForRowAtIndexPath可以从该数组读取必要的值,以便在将来您需要更改部分的大小,只需更新一个地方即可。

暂无
暂无

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

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