【发布时间】:2013-02-15 13:24:57
【问题描述】:
我想用多个部分制作 tableView 但我不想使用字典我有两个数组我希望第一个数组应该在第一部分加载,第二个在第二部分加载。
我有 arrayOne 有 3 个项目,arrayTwo 有 4 个项目,所以如何添加它们并在部分中显示它们。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section == 0)
return resultArray.count;
else
return resultArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 2;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSLog(@"Number of Sections");
if(section == 0)
return @"Section 1";
if(section == 1)
return @"Section 2";
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"Table Cell Data");
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
if (indexPath.section==0) {
appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
ObjectData *theCellData = [resultArray objectAtIndex:indexPath.row];
NSString *cellValue =theCellData.category;
NSLog(@"Cell Values %@",cellValue);
cell.textLabel.text = cellValue;
return cell;
}
else {
ObjectData *theCellData = [resultArray objectAtIndex:indexPath.row];
NSString *cellValue =theCellData.category;
cell.textLabel.text = cellValue;
return cell;
}
}
【问题讨论】:
-
您面临的问题是什么?您的代码似乎正确。
-
单元格中的分配值给出部分错误部分未声明
-
您必须使用 indexPath.section 而不仅仅是 cellForRowAtIndexPath 中的部分。您的其余代码都很好。
-
现在你在控制台中得到了什么值。?还有一件事你不应该在这个函数中创建这个对象 appDelegate。这只会增加内存问题。不是一个好习惯。
标签: iphone ipad uitableview