首页 > 解决方案 > numberOfRowsInSection 重复计算最后一个部分

问题描述

可能重复:表格视图从最后一节开始:numberOfRowsInSection numberOfRowsInSection 从最后一节开始

我的部分代码数量:

- (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。

标签: iosobjective-cuitableview

解决方案


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

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

推荐阅读