【问题标题】:How do I insert text above a tableview [closed]如何在表格视图上方插入文本[关闭]
【发布时间】:2013-11-02 11:52:40
【问题描述】:

我对 ios 设备的应用程序编码完全陌生。我只想知道如何在表格上方插入文本。我已经在 tableViewController 中有一个表。我想在表格上方插入文本。所以它会在表格上方有文本,然后在下方有几个单元格。

我该怎么办?谁能告诉我?

提前致谢。

【问题讨论】:

    标签: ios textview uitableview tableview xcode5


    【解决方案1】:

    将此添加到您的视图控制器类:

    -(void)addTextAbove:(NSString *)textAbove
    {
        for (UIView* subView in self.view.subviews)
        {
            if ([subView isKindOfClass:[UITableView class]])
            {
                UILabel *tv = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
    
                [tv setText:textAbove];
                [tv setBackgroundColor:[UIColor yellowColor]];
                [self.view addSubview:tv];
    
                CGRect frame = subView.frame;
                frame.origin.y = tv.frame.size.height;
                frame.size.height = frame.size.height - tv.frame.size.height;
                [subView setFrame:frame];
            }
        }
    }
    

    在任何你想要的地方调用它:

    [self addTextAbove:@"Txet Abouve"];
    

    【讨论】:

      【解决方案2】:

      您可以使用 UITableView 的 tableFooterView 属性,以便将任何 UIView 放在 tableView 上方。此UIView 将与其余内容一起滚动。

      如果您使用的是 Interface Builder,您可以在 UITableView 的底部边缘放置一个通用的 UIView,Interface Builder 会自动将其设置为 tableFooterView

      如果您正在使用代码创建 UI,您可以开始使用带有所需文本的 UILabel。您可以硬编码所需的高度:

      UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 40)];
      label.text = @"Some text";
      label.textAlignment = NSTextAlignmentCenter;
      self.tableView.tableFooterView = label;
      

      或者你可以告诉UIKit计算高度:

      UILabel *label = [[UILabel alloc] init];
      label.text = @"Some text";
      label.textAlignment = NSTextAlignmentCenter;
      [label sizeToFit];
      self.tableView.tableFooterView = label;
      

      将以下任何代码放入您的 viewDidLoad 方法中。

      【讨论】:

        猜你喜欢
        • 2020-08-17
        • 1970-01-01
        • 2023-03-21
        • 1970-01-01
        • 2013-02-18
        • 1970-01-01
        • 2014-09-29
        • 1970-01-01
        相关资源
        最近更新 更多