我需要创建一个分组的可视化视图,其中包含一些部分,每个部分中可能还包含不同的单元格类型。用户页面(包括'排行榜','朋友建议','朋友','统计','最多探索的类别'...部分)。使用不同的单元格类型创建分组的UITableview
我对ios编程相当新,所以视图可能不是一个分组的uitableview。
我特别坚持的是为部分创建不同的单元格,并找出哪些单元格被点击。
我的数据源将是2个不同的NSArray *,它由不同的数据类型组成,这就是为什么我需要不同的自定义单元格。
我需要创建一个分组的可视化视图,其中包含一些部分,每个部分中可能还包含不同的单元格类型。用户页面(包括'排行榜','朋友建议','朋友','统计','最多探索的类别'...部分)。使用不同的单元格类型创建分组的UITableview
我对ios编程相当新,所以视图可能不是一个分组的uitableview。
我特别坚持的是为部分创建不同的单元格,并找出哪些单元格被点击。
我的数据源将是2个不同的NSArray *,它由不同的数据类型组成,这就是为什么我需要不同的自定义单元格。
既然你有两组不同的数据,你需要在不同的位置显示t部分,您必须将数据源方法分成两部分。
基本上,选择你想成为第一个数据集并离开你。
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(section)return secondArray.count;
//Essentially, if statements evaluate TRUE and move forward if the inside is 1 or greater (TRUE == 1)
return firstArray.count;
//If the first if statement return hits, then the code will never reach this statement which turns this into a lighter if else statement
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section)
{
//do stuff with second array and choose cell type x
}
else
{
//do stuff with first array and choose cell type y
}
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Get the cell with: UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if(indexPath.section)
{
//perform action for second dataset
}
else
{
//perform action for first dataset
}
}
页眉,您可以使用这些方法,只是保持相同类型如上造型:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section;
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
您可以创建UITableViewCell的多个自定义子类,并且在您的UITableViewDataSource的tableView:cellForRowAtIndexPath:方法中,可以使用if语句来确定要使用的单元类型。
例如,这里有什么我可以做一个大概的轮廓:
-(UITableViewCell *)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//First, determine what type of object we're showing
if (indexPath.section == 0) {
//Create and return this cell.
} else if (indexPath.section == 1) {
//Create and return this cell.
}...
}
这里是你会如何实现numberOfRowsInSection
:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section == 0) {
return [firstSectionArray count];
} else if (section == 1) {
return [secondSectionArray count];
} ...
}
对于didSelectRowAtIndexPath
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.section == 0) {
ObjectSelected *objectSelected = [firstArray objectAtIndex:indexPath.row];
//Now you've got the object, so push a view controller:
DetailViewController *dvc = [[DetailViewController alloc] init];
dvc.objectSelected = objectSelected;
[self.navigationController pushViewController:dvc];
} else if (indexPath.section == 1) {
//Same thing, just call [secondArray objectAtIndex:indexPath.row] instead!
}
}
感谢您的回答。好吧它是有道理的,但我怎么能得到这个方法中的项目,我有2个不同的数组,但这种方法只给出索引? 以及如何通过您的方法实现numberOfRowsInSection和didSelectRowAtIndexPath方法? – tackleberry
我不认为你已经明确提出你的问题。你究竟想要做什么以及这个答案有什么问题?这正是您需要做的创建不同类型的单元格。 – RileyE
@RileyE是的,我想你是对的。正如我在我的评论中所说的那样,这可能会解决放置不同类型的单元格的问题,但我仍然不知道如何理解哪个单元格被点击,以及如何将数据传递给它的单击操作。我希望现在很清楚? – tackleberry
随时问我。 – RileyE
我认为你应该澄清indexPath.section和if语句中的段应该与0和1进行比较哦,并使“if(section)return secondArray.count; return firstArray.count;'”if(section = 0){return firstArray.count} else {return secondArray.count}' – erran
谢谢@RileyE。除了为每个部分设置标题之外,现在很清楚。如果你知道这个问题,你可以在你的答案中加入这些信息吗? – tackleberry