UITableView 中的节数会更新,但表视图数据源数组不会更新



我在更新表视图数据源数组时遇到问题。

我想要这样的东西:如果用户通过注册登录,他将在tableView中查看13行

如果他使用 Facebook 登录 ,他将看到 10 行,作为访客,他将看到 5 行。

问题是tableView的行已成功更新,但表视图单元格在不同索引路径上的操作却没有成功更新。

我正在做这样的事情:

 - (NSInteger) numberOfSectionsInTableView : (UITableView *) tableView
   {
     // Return the number of sections.
      if( [SharedGlobal SharedSingleton].userBool==TRUE)
      {
       return 5;
      }
     else if([[[[NSUserDefaults standardUserDefaults]objectForKey:@"profileArrayForGlobal"] valueForKey:@"UserType"] integerValue]==1)
      {
       return 13;
      }
     else
     {
       return 10;
     }
  }

现在cellForRowAtIndexPath

 - (UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath
   {
     TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
     if (cell == nil)
     {
       cell = [[TableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                              reuseIdentifier:@"Cell"];
     }
    if( [SharedGlobal SharedSingleton].userBool==TRUE){
        // do something
    else if([[[[NSUserDefaults standardUserDefaults]objectForKey:@"profileArrayForGlobal"] valueForKey:@"UserType"] integerValue]==1)
    {
    //do something              
       if ([self tableView:tableView canCollapseSection:indexPath.section])
       {
        if (!indexPath.row)
        {
          //do something
           if ([expandedSections containsIndex:indexPath.section])
           {
             // do something
           }
           else
            {
             //do something
            }
         }
      else
      {
        // all other rows
      }
   }
else
  {
        cell.accessoryView = nil;

        cell.lblMenuName.text= [lableArray objectAtIndex:indexPath.section];
        cell.imgMenu.image=[self crop:[UIImage imageNamed: [imageArray objectAtIndex:indexPath.section]]];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
 }

else
    {
      cell.lblMenuName.text= [lableArrayOrg objectAtIndex:indexPath.section];
      cell.imgMenu.image=[self crop:[UIImage imageNamed: [imageArrayOrg objectAtIndex:indexPath.section]]];
      cell.selectionStyle=UITableViewCellSelectionStyleNone;
 }
 return cell;

}

基本上你用错误的方法编写代码,因为你需要根据不同的条件显示行,将方法从

  • (NSInteger) numberOfSectionInTableView : (UITableView *) tableView

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

最新更新