具有动态部分和自定义单元格的UITableView

时间:2014-07-16 13:25:26

标签: objective-c uitableview

我的数据结构如下所示:

NSArray内部我有来自不同类的一些对象。如果像这样的话,数组的外观如何:

让我们说myArray有3 objectA秒。每个objectANSArry都有objectB s

现在在运行时我不知道数组计数。

我的UITableView每个部分显示一个objectA。每个部分有1行。除非我的objectA的NSArray为objectB,且计数大于0.

如果UITableView所有objectA中没有objectB's数组,Section 1: Row 1: `objectA instance Section 2: Row 1: `objectA instance Section 3: Row 1: `objectA instance 就会如此。

objectA

让我们说第3个objectB有一个UITableView数组,其数量为1.我的Section 1: Row 1: `objectA instance Section 2: Row 1: `objectA instance Section 3: Row 1: `objectA instance Section 3: Row 2: `objectB instance 看起来像这样:

UITableViewCell

现在我为每个object使用了两个不同的objectA,因此CustomCell1将使用objectBCustomCell2将使用UITableView

这是我被卡住的地方 - 我需要将正确的单元格返回到正确的部分/行。

所以我确保我的-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { ObjectA *objectA = self.myArray[section]; return [objectA.objectBArray count] +1; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [self.myArra count]; } 知道会发生什么。

我写了这样的方法:

numberOfRowsInSection

所以objectB看一下numberOfSectionsInTableView s数组并返回它的计数+ 1,这样即使它的0总是有一行。 (我在当前部分中至少需要一行objectA。

objectA是直截了当的 - self.myArray

中每个cellForRowAtIndexPath的一个部分

这似乎完全符合我的需要。

然后我陷入 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { CustomCell1 *cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell1"]; CustomCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:@"cell2"]; ObjectA *objectA = self.myArray[indexPath.section]; //Each section has one ObjectA. //Make sure we have some objectB's to display first if ([objectA. objectBArray count] > 0){ ObjectB *objectb = (ObjectB*) objectA.objectB[indexPath.row]; //if the current section (section 3) has a indexPath 0 already // index path 1 would have the first objectB } cell1.objectA = objectA; cell2.objectB = objectB; //Now return the correct cell for the current section / index path } 方法。

这是我走了多远:

objectB

所以我需要检查当前部分是否已经有有效的cell1对象,如果是的话我需要将cell2返回到第二行或第三行。 for numberOfRowsInSection因为第一行必须始终有一个cell1。

我知道我快到了。我只是无法弄清楚如何返回正确的细胞。

更新

所以cellForRowAtIndexPath方法只被调用三次。这是我在 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { ObjectA *objectA = self.myArray[section]; NSLog (@"Section:%ld", (long)section); NSLog(@"Returning %ld", (long)[objectA.objectBArray count]+1); return [objectA.objectBArray count] +1; } 方法中所做的,以确保它返回正确的行数:

2014-07-16 19:36:17.488 App[15473:60b] Section:2
2014-07-16 19:36:17.488 App[15473:60b] Returning 2
2014-07-16 19:36:18.128 App[15473:60b] Section:0
2014-07-16 19:36:18.128 App[15473:60b] Returning 1
2014-07-16 19:36:19.063 App[15473:60b] Section:1
2014-07-16 19:36:19.063 App[15473:60b] Returning 1

我的控制台显示了这个:

{{1}}

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

如果每个部分中始终只有一个objectA,那么您可以测试indexPath.row是否大于零(不是第一行)。

看看这是否有效:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CustomCell1 *cell1 = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
    CustomCell2 *cell2 = [tableView dequeueReusableCellWithIdentifier:@"cell2"];

    ObjectA *objectA = self.myArray[indexPath.section]; //Each section has one ObjectA.
    cell1.objectA = objectA;

    //Check if not first row and objectB's exist
    if (indexPath.row > 0 && [objectA. objectBArray count] > 0){
        ObjectB *objectb = (ObjectB*) objectA.objectB[indexPath.row];
        cell2.objectB = objectB;
        return cell2;
    }

    return cell1;
}

答案 1 :(得分:0)

这是我遇到的第二个问题的答案。在我的视图控制器中设置断点后 - 我注意到我的tableView在调用[self.tableView reloadData]之前已经加载了单元格;

不确定为什么会发生这种情况所以快速检查我的行for items方法解决了这个问题:

如果主数组没有对象 - 返回0.如果确实 - 返回objectBArray count + 1;现在,cellForRowAtIndexPath被称为正确的次数。

相关问题