Iphone 如何使用单个数组获取UITableView节表中的显示数据
我想在表视图中显示服务器上的数据 问题是,我想显示基于类别的数据,所以我有数组类别,其中的类别将是节标题,在它们里面有数据,所以为了显示节中的数据,我必须声明数组 例如如果有三个类别,那么我们必须制作三个数组来填充数据,但如果有更多类别,因为类别是动态的,并且来自服务器,该怎么办Iphone 如何使用单个数组获取UITableView节表中的显示数据,iphone,ipad,nsarray,uitableview,Iphone,Ipad,Nsarray,Uitableview,我想在表视图中显示服务器上的数据 问题是,我想显示基于类别的数据,所以我有数组类别,其中的类别将是节标题,在它们里面有数据,所以为了显示节中的数据,我必须声明数组 例如如果有三个类别,那么我们必须制作三个数组来填充数据,但如果有更多类别,因为类别是动态的,并且来自服务器,该怎么办 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [categoryArray count] ; }
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [categoryArray count] ;
}
以及如何设置节标题的标题,因为它是在类别数组中,所以如果它是数组中的一个接一个节
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSLog(@"Number of Sections");
if(section == 0)
return @"Sales";
if(section == 1)
return @"Soft Skills";
}
如何在tableView单元格中显示数据我可以为所有类别创建数组吗
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (section==0)
{
appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
int count=[resultArray count];
NSLog(@"resultArry Row Counts is %d",count);
return [resultArray count];
}
else{
appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
int count=[resultArrayOne count];
NSLog(@"resultArry Row Counts is %d",count);
return [resultArrayOne count];
}
}
- (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];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
if (indexPath.section==0) {
appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
ObjectData *theCellData = [resultArray objectAtIndex:indexPath.row];
NSString *cellValue =theCellData.sub_Category;
cell.font=[UIFont fontWithName:@"Helvetical Bold" size:14];
NSLog(@"Cell Values %@",cellValue);
cell.textLabel.text = cellValue;
return cell;
}
else {
appDelegate = (MultipleDetailViewsWithNavigatorAppDelegate *)[[UIApplication sharedApplication] delegate];
ObjectData *theCellData = [resultArrayOne objectAtIndex:indexPath.row];
NSString *cellValue =theCellData.sub_Category;
cell.font=[UIFont fontWithName:@"Helvetical Bold" size:14];
NSLog(@"Cell Values %@",cellValue);
cell.textLabel.text = cellValue;
return cell;
}
}
请尝试下面的代码。它将解决您的问题:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [categoryArray objectAtIndex:section];
}
编辑:
numberOfRowsInSection
和cellforrowatinexpath
委托函数同样,将每个类别的值存储在nsmutable数组NSDictionary中,并在uitableviewcell中显示每个类别的数据。由于从服务器获取类别似乎不是您的问题,我的答案基于预填充数组,以便更好地可视化
NSMutableArray *categories = @[@"Cat1", @"Cat2"];
// creata a dictionary with all the array for the categorie rows
NSMutableDictionary *rowDict = @{@"Cat1":@[@"Cat1Row1",@"Cat1Row2",@"..."],
@"Cat2":@[@"Cat2Row1", @"Cat2Row2",@"..."]
此解决方案的关键是使用类别字符串作为字典的键
您现在可以像这样访问标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return categories[section];
}
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// ...
// create or deque a cell like you normally would do
// now configure the cell
cell.textLable.text = [rowDict[categories[indexPath.section]] objectAtIndex:indexPath.row]
}
然后像这样访问您的行
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return categories[section];
}
- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// ...
// create or deque a cell like you normally would do
// now configure the cell
cell.textLable.text = [rowDict[categories[indexPath.section]] objectAtIndex:indexPath.row]
}
但是如何在tableview中显示可能会创建arrayyes@Rushi,但是如何根据sections@ShahzadAli你能给我看一下cellforrowatindexpath的代码吗?是的,我有模型类ObjectData,它存储结果数组,但是如何显示每个部分的项目不同,有什么想法吗?你能帮我做吗但如何在tableView单元格中显示每个部分的数据我必须创建三个数组或在单个数组中,我们可以显示所有的内容scategories都来自服务器,因此我们不能给出固定值,我们将如何在字典中添加,以及如何显示sectionscategories中的行数来自服务器,因此我们不能给出固定值和我们将如何在字典中添加,以及如何在章节中显示行数我知道-但我不知道您是如何获得它们的,所以我选择“填充”“由于获取阵列似乎不是你的问题,因此它们可以更好地可视化。你可以从服务器动态获取阵列。这并不重要。我的解决方案解决了你需要访问动态类别计数的问题。对于标题和单元格。