我正在尝试创建一个包含动态数据的表,但有点卡住了。这是我的数据的结构:
NSMutableArray *bigArray;
bigArray
有许多 NSDictionary
项。
每个 items
只有一个条目。
sectionName
是键,NSMutableArray 是值。
value
NSMutableArray 中有很多对象。
我试图尽可能简单地解释这一点,这是我卡住的部分。
//easy
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [bigArray count];
}
//medium
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [[[[bigArray objectAtIndex:section] allValues] objectAtIndex:0] count];
}
我不知道这部分如何根据我当前的数据结构实现这个方法:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"MyCell"];
MyObject *obj = //Need this part
cell.textLabel.text = obj.name;
return cell;
}
简单地说,我正在尝试插入带有动态数据的动态部分。我正在向更有经验的开发人员寻求建议,您会如何解决这个问题?
最佳答案
假设我很好地理解了您的数据的结构,那么我会这样做:
(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyCell"];
}
//this will get you the dictionary for the section being filled
NSDictionary *item = [bigArray objectAtIndex:indexPath.section];
// then the array of object for the section
NSMutableArray *mutableArray = [item objectForKey:@"sectionName"];
//you then take the object for the row
MyObject *obj = [mutableArray objectAtIndex:indexPath.row];
cell.textLabel.text = obj.name;
return cell;
}
不要忘记在属性检查器中为单元格原型(prototype)设置重用标识符
关于ios - 关于在IOS中创建动态数据表的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10229296/