iphone - 具有多个部分的 UITableView

标签 iphone ipad uitableview

我想使 tableView 具有多个部分,但我不想使用字典,我有两个数组,我希望第一个数组应加载在第一部分中,第二个数组应加载在第二部分中。

我有包含 3 个项目的 arrayOne 和包含 4 个项目的 arrayTwo,那么如何添加它们并按部分显示它们。

- (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;       
     }
}

最佳答案

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
      return 2 ;
 }

 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
      if (section==0)
      {
             return [array1 count];
      }
      else{
             return [array2 count];
      }
 }

 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
      if(section == 0)
           return @"Section 1";
      else
           return @"Section 2";
 }


 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{

      static NSString *CellIdentifier = @"Cell";

      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
     if (cell == nil) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
      }

 if (indexPath.section==0) {
     ObjectData *theCellData = [array1 objectAtIndex:indexPath.row];
     NSString *cellValue =theCellData.category;
     cell.textLabel.text = cellValue;
 }
 else {
     ObjectData *theCellData = [array2 objectAtIndex:indexPath.row];
     NSString *cellValue =theCellData.category;
     cell.textLabel.text = cellValue;
 }
     return cell;        
 }

关于iphone - 具有多个部分的 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15151037/

相关文章:

iphone - 如何传递带有 postNotificationName :object: 的 NSDictionary

iphone - iOS 模拟器在虚拟机中崩溃

ios - 在 iPhone 上启动我的应用程序时出现黑屏

ios - 更改父 View Controller 的界面方向

iPhone 内存管理和释放

ios - Swift Csv 解析器对双引号的支持

ios - Core Data 和 Sqlite 用于大数据的工作?

ios - 试图从 UITableView 返回选定的单元格,但它只返回 nil

ios - 填充 TableView 中的 JSON 数组和 CoreData

ios - 如何使用自定义单元格重新排列 UITableView?