2012-09-28 69 views
0

我希望UITableView能够在分隔的部分显示单元格(它们之间有距离,空格)。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

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

+0

你可以试试这个:http://stackoverflow.com/questions/4804632/uitableview-separator-line或本:http://stackoverflow.com/questions/3521310/how-to-increase-the -uitableview-分隔符高度 – brainray

回答

0

如果表格有空白分隔符(即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; 
} 
相关问题