numberOfSectionsInTableView用于同一控制器中的2个不同单元

时间:2014-02-02 18:22:45

标签: ios objective-c uitableview

我有一个有2个独立单元格的视图。一切正常,除了每个单元格必须不同的numberOfRowsInSection返回。

我希望我的单元格“虚拟”只返回1或2.

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //I need to add some code in here that will say that
    //if my cell = dummy then return 1 else

    //Returning table count based on number of Team Names
    return _teamNames.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && indexPath.row == 0) {
        return indexPath.row + 20 - indexPath.row;
    }

    if (indexPath.section == 0 && indexPath.row == 18) {
        return indexPath.row + 50 - indexPath.row;
    } else {
        return indexPath.row + 26 - indexPath.row;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *dummycell = @"dummy";
    dummytest *cellar = [tableView dequeueReusableCellWithIdentifier:dummycell 
                                                        forIndexPath:indexPath];

    cellar.righthere.text = @"hello";
    static NSString *CellIdentifier = @"StandingsIdent";

    StandingsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier 
                                                              forIndexPath:indexPath];

有什么想法吗?谢谢。

2 个答案:

答案 0 :(得分:1)

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{



// I need to add some code in here that will say that if my cell = dummy then return 1 else

// You need to set this bool value in your code somewhere - where you have some sort of toggle button between - Dummycell and StandingsViewCell
    if(isDummyCell) 
    {
        return 1;
    }
    else
    {
       return _teamNames.count; // Returning table count based on number of Team Names
    }


}

答案 1 :(得分:0)

您可以使用NSMutableDictionary作为Table View数据的源(即“MyData”),这样您就可以直接通过数据源控制每个单元格的行为。

- (NSInteger)tableView:(UITableView *)tableView 
 numberOfRowsInSection:(NSInteger)section {

    //list of elements
    if (self.keys.count == 0) {
        return 0;
    }

    NSString *key = [self.keys objectAtIndex:section];
    NSArray *mySection = [MyData objectForKey:key];
    return [mySection count];

}

其中keys是包含您的部分的数组。当您加载数据时,如果您想要按字母顺序排列,则只需执行以下操作:

NSMutableArray *keyArray = [[NSMutableArray alloc] init];
[keyArray addObjectsFromArray:[[self.MyData allKeys] sortedArrayUsingSelector:@selector(compare:)]];
self.keys = keyArray;