【问题标题】:Separated cells in UITableViewUITableView 中的分隔单元格
【发布时间】:2012-09-28 13:44:55
【问题描述】:

我希望UITableView 显示分隔部分中的单元格(它们之间有距离、空格)。

所以我想出了这个:

InventoryViewController.m

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [[[InventoryStore sharedInventory] allInventories] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Inventory *p = [[[InventoryStore sharedInventory] allInventories]
                  objectAtIndex:[indexPath section]];

    StepperCell *cell = [tableView
                         dequeueReusableCellWithIdentifier:@"StepperCell"];

    [cell setController:self];
    [cell setTableView:tableView];

    [[cell nameLabel] setText:[p inventoryName]];
    [[cell valueLabel] setText:
     [NSString stringWithFormat:@"$%d", [p value]]];
    [[cell quantityLabel] setText:
     [NSString stringWithFormat:@"%d", [p quantity]]];
    cell.stepper.value = [p quantity];

    return cell;
}

当然还有 AppDelegate.m

InventoryViewController *inventoryViewController = [[InventoryViewController alloc] initWithStyle:UITableViewStyleGrouped];

我的问题是,有没有更好或更简单的方法来分隔单元格,但在同一部分? 我想要一个部分,并从一个数组中提取数据,但是这些单元格之间应该有距离。

【问题讨论】:

标签: iphone uitableview distance cells


【解决方案1】:

如果表格有空白分隔符(即 separatorStyle 设置为 UITableViewCellSeparatorStyleNone),那么您可以在每个单元格之间注入空白单元格。

例如

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 if (indexPath.row % 2) {
 /* Your cell rendering code goes here */
 }
 else {
  UITableViewCell * blankCell = ....;
 }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return (numberOfInventoryItems * 2) - 1;
}

【讨论】:

    猜你喜欢
    • 2011-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多