我正在使用一个使用动态部分的 TableView ,我有一个包含部分标题的 sectionArray 和另一个包含所有部分的全部数据的数组, 我不知道如何为每个部分的行编写单元格,因为它是动态的
代码是:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [appDelegate.sectionArray count];
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [appDelegate.appNameArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
UILabel *AppNameLabel=[[UILabel alloc]initWithFrame:CGRectMake(80, 0, 120, 30)];
[AppNameLabel setBackgroundColor:[UIColor clearColor]];
[AppNameLabel setText:[NSString stringWithFormat:@"%@",[appDelegate.appNameArray objectAtIndex:indexPath.row]]];
[AppNameLabel setFont:[UIFont systemFontOfSize:14]];
[cell.contentView addSubview:AppNameLabel];
return cell;
}
在 appname 数组中,我有包含所有部分的数据,我需要为特定部分进行隔离,如何在行索引的单元格中创建它,请帮助。
提前致谢。
最佳答案
无需为 sectionTitle 和 appName 创建两个不同的 array
。使用对象的 NSDictionary
创建源 NSArray
。 NSDictionary
对象包含作为 values
的 appName 数组和作为 key
的 sectionTitle。
sourceArray
(
dictionaryObject1
{
key : sectionTitle,
value : ( appName1,appName2,appName3)
},
dictionaryObject2
{
key : sectionTitle,
value : ( appName1,appName2,appName3)
},
.
.
.
)
关于ios - UITableView 动态部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18765300/