【问题标题】:Update section footer title in UITableView without reloading在 UITableView 中更新节页脚标题而不重新加载
【发布时间】:2011-08-21 05:49:10
【问题描述】:

我希望能够在通过键盘输入文本时以编程方式更新表格视图中某个部分的页脚标题。当我单击一个单元格以使其详细文本视图可编辑时会出现键盘,因此我想在不从数据源重新加载的情况下更新页脚标题。

事实上,如果我这样做了,键盘就会消失,所以这不是一种好的交互方式。我一直无法找到解决这个问题的好方法...有什么建议吗?

谢谢

【问题讨论】:

    标签: iphone ios uitableview


    【解决方案1】:

    如果你有分组表,你可以使用:

    - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
        yourLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 17)]; //I'm not sure about the frame...
        yourLabel.font = [UIFont systemFontOfSize:16];
        yourLabel.shadowColor = [UIColor whiteColor];
        yourLabel.shadowOffset = CGSizeMake(0, 1);
        yourLabel.textAlignment = UITextAlignmentCenter;
        yourLabel.textColor = RGB(76, 86, 108);
        yourLabel.backgroundColor = [UIColor clearColor];
        yourLabel.opaque = NO;
        return yourLabel;
    }
    

    在您的 .h 文件中声明 yourLabel。然后,您可以通过

    访问它
    yourLabel.text = @"whatever you want";
    

    请检查是否有效:)

    【讨论】:

    • 是的,它可能会起作用。但是我将不得不处理大量的组,所以处理所有这些不同的标签对我来说并不容易。我真的会尝试以一种干净的方式来做......
    • 顺便问一下,yourFrameCGRect 实例应该放入什么维度?
    • 可以在NSArray中放置标签,然后使用for循环。
    • 糟糕,我忘记了框架。我想应该是CGRectMake(0, 0, 320, 17)
    • 嗯,我会试一试...但是,要使它与 iPad 兼容,我应该放什么框架?还是我应该只处理UILabelautoresizingMask
    【解决方案2】:

    我知道我来晚了,但我发现你可以这样做:

    [self.tableView beginUpdates];
    [self.tableView footerViewForSection:section].textLabel.text = @"Whatever";
    [[self.tableView footerViewForSection:section].textLabel sizeToFit];
    [self.tableView endUpdates];
    

    【讨论】:

      【解决方案3】:

      对于第一部分(ziro)

      [self.tableView footerViewForSection:0].textLabel.text = @"test";

      【讨论】:

        【解决方案4】:

        Mark Alldritt 的回答很好,但有时标签大小不正确。固定示例如下(对于部分 == 0):

        [self.tableView beginUpdates];
        UILabel *footerLabel = [self.tableView footerViewForSection:0].textLabel;
        
        footerLabel.text = [self tableView:self.tableView titleForFooterInSection:0];
        CGRect footerLabelFrame = footerLabel.frame;
        footerLabelFrame.size.width = self.tableView.bounds.size.width;
        footerLabel.frame = footerLabelFrame;
        [self.tableView endUpdates];
        

        【讨论】:

          【解决方案5】:

          斯威夫特 5 答案

          tableView.beginUpdates()
          
          tableView.footerView(forSection: 0)?.textLabel?.text = "Text Here"
          
          tableView.endUpdates()
          

          【讨论】:

            猜你喜欢
            • 2018-03-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-08-07
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多