numberOfRowsInSection 重复计算最后一个部分

Posted

技术标签:

【中文标题】numberOfRowsInSection 重复计算最后一个部分【英文标题】:numberOfRowsInSection repeats counting the last section 【发布时间】:2018-06-07 06:49:24 【问题描述】:

可能重复:Table View starts with last section in: numberOfRowsInSection numberOfRowsInSection starts with last section

我的段码数:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    //#warning Incomplete method implementation.
    // Return the number of rows in the section.

 //   NSLog(@"outsideindx%d", indexsection);
    for (int indexsection = 0 ;  indexsection < [arrayOfArrays count ]  ; indexsection ++)
    

        NSLog(@"outsidesection%d",section);

        if([buttonTags containsObject:@(section)])
        
            if (section == indexsection)
            
                 NSLog(@"HIDING!");
                return 0;
            
            else
            
             return [arrayOfArrays [indexsection] count];
            

        

      else
          if(section == indexsection)
        
            //stars with 1 to 0
            return [arrayOfArrays [indexsection] count];
        
    

我的代码在我的标题中隐藏了按钮。 buttonTags 是一个包含被点击按钮的数组。 所以我想做的是隐藏被点击的按钮。 如果 buttonTag 的值为 button[1],则 == 1 的部分将被隐藏。

如果 buttonTags 包含 [ 0 , 1] 则部分 0 和 1 将被隐藏。

但结果破坏了我的代码的想法 结果从 1 开始,我点击了按钮 [0]。

结果是这样的:

insidesection: 1
insidesection: 1
insidesection: 0
outsidesection: 1
outsidesection: 0

部分结果与发布的重复项不同,

我的部分数量在这里:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  //#warning Potentially incomplete method implementation.
  // Return the number of sections.
  return 2;

我需要从 0 到 1 开始计数部分。而不是从 1 到 0。

【问题讨论】:

numberOfRowsInSection 代码不完整 @RajeshKumarR 已编辑, 1.您没有理由关心numberOfRowsInSection 的调用顺序。 2.为什么numberOfRowsInSection中有for循环? numberOfRowsInSection 中的 return 语句在哪里 @rmaddy 更新了帖子 【参考方案1】:

当然,您只想查看相关部分,而不是遍历所有部分。比如:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
    if([buttonTags containsObject:@(section)]) 
        return 0;
     else 
        return [arrayOfArrays[section] count];
    

【讨论】:

以上是关于numberOfRowsInSection 重复计算最后一个部分的主要内容,如果未能解决你的问题,请参考以下文章