【问题标题】:Using a static UITableViewController to create two sections with the first section having a non-static number of cells使用静态 UITableViewController 创建两个部分,第一部分具有非静态数量的单元格
【发布时间】:2015-08-12 13:23:59
【问题描述】:

我有一个UITableViewController,我正在我的应用程序中构建它。这个UITableViewController (SelectedLanguages) 是从另一个UITableViewController (ChooseLanguage) 调用的,其中有一个静态的语言列表供用户选择。

SelectedLanguagesUITableViewController,我想实现如下:

  • 2 节
  • 第一部分将包含 1 到 5 个单元格
  • 第二部分总是有 12 个单元格。

第一部分的单元格数量完全取决于用户在ChooseLanguageUITableViewController 中选择的语言。例如,点击英文将意味着SelectedLanguage UITableViewController 中的第一部分将有5 个单元格,但在ChooseLanguage UITableViewController 中选择French 将意味着SelectedLanguage 中的第一部分将只有1 个单元格。

如前所述,第二部分将始终在 SelectedLanguage 中包含 12 个单元格。

我在Interface Builder 中设计了这个UITableViewController。我所看到的是,只有将内容指定为Static Cells,您才能拥有多个“部分”。

即使您将内容设置为动态和分组,我似乎也无法找到一种方法来确定代码以外的部分。

我在Interface Builder 中定义它的原因是因为第 1 节和第 2 节需要对单元格大小以及进入每个单元格的标签进行非常自定义的布局。

第一部分的内容不是动态的;它是静态的,因为在构建这个应用程序时,我会确切地知道每种语言的第一部分应该有多少个单元格,所以在我看来,使用静态单元格是正确的。

我的问题是,如何在代码中设置顶部单元格的数量?

在 ChooseLanguage 的 prepareForSegue 中,我可以检查调用的单元格标题,然后在 SelectedTransactions 中执行一些操作。在这里执行的操作是我感到困惑的。

我知道 UITableView Data Source 中有 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 方法,但我不确定如何以及如何处理我的特殊情况。

任何指导将不胜感激。

【问题讨论】:

    标签: ios objective-c iphone uitableview


    【解决方案1】:

    我对您的问题最简单的答案如下

    在.m 导入“ViewController.h”

         @interface ViewController () 
         {
           NSMutableArray *arraysection1;
           NSMutableArray *arraysection2;
    
         }
         @end
    
         @implementation ViewController
    
         - (void)viewDidLoad 
         {
            [super viewDidLoad];
            // Do any additional setup after loading the view, typically from a nib.
            arraySection1 = [[NSMutableArray alloc]initWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",nil];
            arraySection2 = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",nil];
         }
    
      #UITableView Delegate Methods
    
         #UITableView DataSource Methods
         - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
         {
            //If you have 2 sections,
            return 2;
         }
         -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
         {
            //set row of two sections with condition
            if(section==0)
            {
               return arraySection1.count;
            }
            else
            {
               return arraySection2.count;
            }
    
         }
         -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
         {
            static NSString *strCellIdentifier = @"CellIndentifier";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strCellIdentifier];
            if (cell == nil) 
            {
               cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strCellIdentifier];
           }
    
           if(indexPath.section==0)
           {
              cell.textLabel.text = [NSString stringWithFormat:@"%@",[arraySection1 objectAtIndex:indexPath.row]];
              NSLog(@"The textLabel is-%@",cell.textLabel.text);
           }
           else
           {
             cell.textLabel.text = [NSString stringWithFormat:@"%@",[arraySection2 objectAtIndex:indexPath.row]];
             NSLog(@"The textLabel is-%@",cell.textLabel.text);
           }
           return cell;
         }
    
        #UITableView Delegate Methods
        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
        {
           if(indexPath.section==0)
           {
              if(indexPath.row==0)
              {
                //Do whatever you want here
              }
              else if(indexPath.row==1)
              {
                //Do whatever you want here
              }
              else if(indexPath.row==2)
              {
                //Do whatever you want here
              }
              else if(indexPath.row==3)
              {
                //Do whatever you want here
              }
              else 
              {
                //Do whatever you want here
              }
    
           }
           else
           {
              if(indexPath.row==0)
              {
                //Do whatever you want here
              }
              else if(indexPath.row==1)
              {
                //Do whatever you want here
              }
              else if(indexPath.row==2)
              {
                //Do whatever you want here
              }
              else if(indexPath.row==3)
              {
                //Do whatever you want here
              }
              else 
              {
                //Do whatever you want here
              }
           }
        }
    

    【讨论】:

    • 非常感谢您的回复。我还可以使用 IB 创建自定义子标签等(不必在代码中更新)并以相同的方式使用您的代码吗?
    • 是的,使用我发布的代码。因为我根据您的问题编写了静态单元格。
    • 绝对可以得到解决方案。
    • 如果我的代码对你有帮助,请打勾并给我打分,因为如果他们看到你的问题,它对其他人非常有用。
    • 非常感谢 - 这是有道理的,感谢您的帮助。
    【解决方案2】:

    从您的描述看来,您的 tableView 应该是动态的。

    你必须以编程方式处理这个,忘记界面构建器

    这是你需要的:

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return 2;//2 Sections
    }
    
    
    -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
        switch (section) {
            case 0:
                //The first section will have between 1 and 5 cells
                //Put the logic to return the correct number of cells
                return 5;
                break;
            case 1:
                //The second section will always have 12 cells.
                return 12;
                break;                
            default:
                break;
        }
        return 0;
    }
    
    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
        switch (indexPath.section) {
            case 0:
                [[cell textLabel] setText:@"same title section 1"];
                break;
            case 1:
                [[cell textLabel] setText:@"same title section 2"];
                break;
    
            default:
                break;
        }
        return cell;
    }
    

    【讨论】:

    • 非常感谢@meda - 这非常有帮助。我是编程的新手,我不想在代码上乱七八糟,因为我可以在 Interface Builder 中做一些事情。第二部分中的 12 个单元格将始终具有相同的标题,而第一部分将始终包含所有 5 个相同的标题;这只是我显示多少的一个例子。所以在你那里的代码中,在你说要放置逻辑的行中,我可以根据我在上一个 UITableView 中单击的单元格放置正确数量的单元格吗?
    • 你知道界面不是那么灵活,我更新了我的代码来告诉你如何放置标题。至于逻辑,它可能基于条件或您的数据源数组计数。
    • 非常感谢,非常感谢,我确实会努力理解在代码中这样做。感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 2014-06-18
    • 2012-12-22
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多