【问题标题】:display the right section of tableview显示表格视图的右侧部分
【发布时间】:2023-04-02 06:46:01
【问题描述】:

我有一个表格视图,我正在用两个 UIButtons 切换它的 dataDource(如选项卡)。

- (IBAction)dataToggler:(id)sender {
    int buttonTag = ((UIButton*)sender).tag;

    if(_firstTab.selected && buttonTag != 1){

        AppDelegate *myAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        myAppDelegate.isFeatured = TRUE;
        [myAppDelegate loadEvents];

        numberOfSections = 1;

        _firstTab.selected = NO;
        _secondTab.selected = YES;

    } else if(_secondTab.selected && buttonTag != 2){

        AppDelegate *myAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        myAppDelegate.isFeatured = FALSE;
        [myAppDelegate loadEvents];

        numberOfSections = 2;

        _firstTab.selected = YES;
        _secondTab.selected = NO;
    }
    [tableView reloadData];

}

每次单击其中一个按钮时,我的 xml 都会被解析并添加到单独的数组中;数组包含一个事件列表(例如,第一部分包含现在的事件,第二部分包含其余事件)。

我遇到的问题是,当我选择第二个按钮时,我有两个部分,但有时我的第一个部分是空的,所以如果它是空的,我不想显示该部分;请快速查看我的代码文件...

现在,如果我的第二个选项卡(按钮)被选中,我将得到一个空表...

我的代码文件是:myViewController 和 myAppDelegate

【问题讨论】:

    标签: iphone ios5 tableview uitableview


    【解决方案1】:

    在你的 tableView 数据源中,实现这个方法:

     - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
            return 2;
       }
    
    
     - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
         if ( [self tableView:tableView numberOfRowsInSection:section] > 0 ) {
             // INSERT YOUR HEADER STRING "times" array here.
             return [NSString stringWithFormat:@"section %d",section];
         } else {
             return nil;
         }
      }
    

    【讨论】:

    • 但是对于空的情况,我只想有一个部分;实际上,如果计数为 0,则第二个和第一个消失...
    • 已经这样做了,但是在选择第一个选项卡时出现错误,得到一个带有我的第一部分标题的空表;有什么线索吗?
    • 啊,在你的 numberOfRowsInSection 和 cellForRowAtIndexPath 中,你需要调整节号为 0 或 1。换句话说,如果只有一个节,在这些方法中它将是节 0 ,而不是第 1 节。如果有 2 个节,它们将是第 0 节和第 1 节。
    • 你能举个例子吗?基于my app
    • 好的,你总是有 2 个部分。当第 0 部分为空时,您的 numberOfRowsInSection 应返回 0。如果您看到此部分的 title,则您的 titleForHeaderInSection 还必须为标题返回 @""。请参阅上面的代码示例以获取 titleForHeaderInSection。
    【解决方案2】:

    如果是我,我可能会将我的数据存储在二维数组中,那么您将能够拥有以下内容,

    • (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { // 返回部分的数量。 返回 [self.2darray 计数]; }

    • (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 返回 [[self.2darray objectAtIndex:section] count]; }

    这将为您提供正确数量的部分和行。而不是试图弄乱你在这些方法中有多少部分我会添加,

    • (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if([[self.2darray objectAtIndex:section] count]

      返回 44; }

    这相当整洁,无论您创建的行数和节数如何,它都可以正常工作。希望这会有所帮助。

    【讨论】:

    • 另外,根据您正在做什么,您可能希望有两个单独的数据源,每个数据源都实现 UITableViewDataSource 并在它们之间通过在切换按钮时更改 tableView 的数据源来实现。这将是一个更具体的情况,通常我只会在表示的数据不同时才这样做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多