2013-03-12 236 views
0

我想要什么:在表格视图中显示一些数据列表。带空单元格的Tableview

通过创建不同的对象将数据存储在单个数组中。因此,对于每个对象,数组的大小都不相同。所以我的问题是显示数据,而不是完全填充表格单元格,一些单元格留空。它看起来不太好。

任何人都可以解释如何得到一个表视图没有任何额外的空单元格?

这是我如何安排数据:

Recipe *recipe1 = [Recipe new]; 


recipe1.ingredients = [NSArray arrayWithObjects:@"1. Fish - 500 g .",@"2. Onion (Big) - 2 nos (Chopped)",@"3. Garlic (Chopped) - 3 flakes",@"4. Green chillies - 2 nos (Chopped)",@"5. Chilly powder - 3/4 tbsp ",@"6. Coriander powder - 1/2 tsp ",nil]; 


Recipe *recipe2 = [Recipe new]; 

recipe2.ingredients = [NSArray arrayWithObjects:@"1. fish - 300 gm",@"2. Tomato(medium) - 1 no",@"3. Garlic - 10 gm",@"4. Coconut powder - 10 gm",@"5. Curry leaves - A few",@"6. Salt - As reqd",@"7. Onions(medium) - 2 nos(chopped)",@"8. Oil - 1 - 2 tbsp",nil]; 

我想说明的tableview取决于哪个菜被选中里面这些成分列表。

这里是我的数据源方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)aTableView { 
    // Return the number of sections. 
    return 1; 
} 


- (NSInteger)tableView:(UITableView *)aTableView numberOfRowsInSection:(NSInteger)section { 
    // Return the number of rows in the section. 
    return [data count]; 
} 


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"CellIdentifier"; 

    // Dequeue or create a cell of the appropriate type. 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     cell.accessoryType = UITableViewCellAccessoryNone; 
    } 

    [cell.textLabel setFont:[UIFont fontWithName:@"Bold" size:20]]; 

    // Configure the cell. 
    cell.textLabel.text =[data objectAtIndex:indexPath.row]; 
    return cell; 
} 
+0

将ü张贴的TableView的数据源方法? – 2013-03-14 09:48:38

回答

0

cellForRowAtIndexPath方法尝试使用以下

在第二个配方选择:

cell.textLabel.text = [recipe1.ingredients objectAtIndex:indexPath.row]; 

在第二个配方选择:

cell.textLabel.text = [recipe2.ingredients objectAtIndex:indexPath.row]; 
0

修改以下功能:对(MAC OS)

- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView 
    { 
     if (First Recipe) 
    return [recipe1.ingredients count]; 

     else if (Second Recipe) 
    return [recipe2.ingredients count]; 

    } 

或为iOS:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    { 
     if (First Recipe) 
      return [recipe1.ingredients count]; 

       else if (Second Recipe) 
      return [recipe2.ingredients count]; 
    } 

之后一定要重新加载表视图,一旦选择完成。 愿这能帮助你。

+0

有没有这样的方法名为numberOfRowsInTableView – haritha 2013-03-12 09:47:03

+0

检查并学习:https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Protocols/NSTableDataSource_Protocol/Reference/Reference.html – 2013-03-12 10:04:34

+0

我已更新答案..现在检查。 – 2013-03-13 06:23:19

0

你想要做的是:

你将有阵全配方的对象&这将是你的tableview的数据源。 &当你点击任何一个食谱(名称)时,它会推送到另一个视图控制器,在tableview(列表)中给出该食谱所需的配料列表。

为此,首先创建一个带有recipeArray(配方对象)的主视图控制器作为数据源&列出带有配方名称的表视图中的配方。当你选择一个配方(在didSelectRowAtIndexPath中被调用)时,推送到新的视图控制器,所选配方对象&填充下一个tableview控制器中的成分列表。

0

在viewDidLoad中添加

tableview.tableFooterView = [[UIView alloc] init]; 

1号线,并添加下面这个方法,所以你不会看到任何额外的细胞,其是空的

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Remove seperator inset 
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) { 
     [cell setSeparatorInset:UIEdgeInsetsZero]; 
    } 

    // Prevent the cell from inheriting the Table View's margin settings 
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) { 
     [cell setPreservesSuperviewLayoutMargins:NO]; 
    } 

    // Explictly set your cell's layout margins 
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) { 
     [cell setLayoutMargins:UIEdgeInsetsZero]; 
    } 
}