【问题标题】:UITableView With Multiple Sections具有多个部分的 UITableView
【发布时间】: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


【解决方案1】:

更改此if (section==0)

if (indexPath.section==0)

你必须实现这个功能:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
 {
     if(section == 0)
         return arrSection1.count;
      else
         return arrSection2.count;
 }

【讨论】:

  • 再次出现错误,我已经这样做了 indexPath.section
  • @NaziaJan 你实现了这个功能吗?
【解决方案2】:

您的numberOfRowsInSection 应该是这样的:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return firstArray.count;
    } else {
        return secondArray.count;
    }
}

【讨论】:

    【解决方案3】:

    你几乎走在正确的轨道上......

    让我们考虑您的两个数组是arrOnearrTwo.....

        - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
          if(section == 0)
          return [arrOne count];
          else
          return [arrTwo count];
        }
    
        - (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 = [arrOne objectAtIndex:indexPath.row];
            NSString *cellValue =theCellData.category;
            NSLog(@"cellValue = %@",cellValue); //To see the value of string cellValue
            cell.textLabel.text = cellValue;
    
            return cell;
    
        }
    
        else {
            ObjectData *theCellData = [arrTwo objectAtIndex:indexPath.row];
            NSString *cellValue =theCellData.category;
            cell.textLabel.text = cellValue;
    
            return cell;        
        }
    }
    

    【讨论】:

    • 我猜 P 应该是 indexpath 中的大写字母,.... 已经编辑过了。现在试试,应该可以了。
    • 我已经添加了一行代码,将其添加到您的代码中,看看您的字符串中是否获得了正确的值以打印在您的单元格标签中。尝试一次。
    【解决方案4】:
     - (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;        
     }
    

    【讨论】:

    • 这 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { if (indexPath.section==0) { return [array1 count]; } else{ 返回 [array1 计数]; } } 是错的。您无法在此处获取 indexPath.section。
    • 你能用 NSLog 你的 cellValue 看看里面有没有数据吗
    • 它没有显示我们编写 cellForRowAtIndex 方法的任何内容的 NSLog,除了在部分和行数中 NSLog 正在工作,因此这意味着它没有进入 rowForIndexPath
    • 好答案...您只需要更改 else 语句以返回 array2 而不是 array1 的计数。
    【解决方案5】:

    TableView Delegate Method 已经将部分指定为 :(NSInteger)section.. 所以我们也可以使用 switchCase

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
        {
            switch (section)
            {
                case 0:
                    return self.arrMedia.count;
                    break;
                case 1:
                    return 1;
                    break;
            }
            return 0;
        }
    

    【讨论】:

      【解决方案6】:

      以下是相关的 Swift 3 行:

      func numberOfSections (in tableView: UITableView) -> Int {
          ...
          return numberOfSections
      }
      
      func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
          //Not necessary, but will let you format the section headers. Example:
          guard let header = view as? UITableViewHeaderFooterView else { return }
          header.textLabel?.font = UIFont.boldSystemFont(ofSize: 24)
          header.textLabel?.textColor = UIColor.darkGray
          header.textLabel?.textAlignment = .center
          header.textLabel?.frame = header.frame
          view.tintColor = UIColor.white
      }
      
      func tableView(_ tableView: UITableView, willDisplayFooterView view: UIView, forSection section: Int) {
          //Also not necessary, but clear footers help space out sections
          view.tintColor = UIColor.clear   
      }
      
      func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
          //Spacing between sections:
          return 25
      }
      
      func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
          ...
          return sectionCount
      }
      
      func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
          //All your cell setup
          ...
          //I call this to get the absolute row number (disregarding sections)
          let row = getAbsoluteRow_InCurrentSection(indexPath: indexPath)
          //Now you can use the row number to grab a value from an array or CoreData object and use the value to populate your cell!! 
      }
      
      func getAbsoluteRow_InCurrentSection(indexPath: IndexPath) -> Int {
          var aggRows = 0
          if currentListEntity == "GrocTask" {
              let curSection = indexPath.section
              for i in 0..<curSection {
                  aggRows += flex1ArrayCnt[i]
              }
              aggRows += indexPath.row
          }
          return aggRows
      }
      

      //请注意,我只包括那些与构建带有部分的 UITableView 直接相关的内容。我没有包含 editActions、didSelect 或其他 UITableView 委托函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-22
        • 1970-01-01
        相关资源
        最近更新 更多