【问题标题】:removing the extra lines in uitableview using swift [duplicate]使用swift删除uitableview中的多余行[重复]
【发布时间】:2016-03-26 05:47:01
【问题描述】:

我正在学习使用 Swift 和 Xcode 制作 IOS 程序。我一边看书一边看视频。我遇到了一个问题。我在视图控制器中创建了一个表视图。我知道如何指定行数以及文本标签中的内容。但是,我不知道如何删除 tableview 产生的额外行。我只有 4 行,但它显示了空白的额外行。

这是我的代码:


import UIKit /// imports the UIKit library

class MainScreenViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var MainScreenTableView: UITableView!
    let myMenuOptions = [ "Instructions", "Create", "Open", "Send" ]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        self.MainScreenTableView.dataSource = self // self refers to the view controler we are in
        self.MainScreenTableView.delegate = self   /* dataSource means the view control provides the table with information */
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.myMenuOptions.count
    }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = UITableViewCell()
        cell.textLabel!.text = myMenuOptions[indexPath.row]
        return cell
    }

谢谢

【问题讨论】:

    标签: ios swift uitableview


    【解决方案1】:

    您需要在表格视图中添加一个空页脚以删除多余的行。在viewDidLoad中,添加如下代码:

    let footer = UIView(frame: CGRectZero)
    self.MainScreenTableView.tableFooterView = footer
    

    有关表格视图页脚的更多信息,请参阅UITableView documentation

    【讨论】:

      【解决方案2】:

      UITableView 有点烦人,但很容易解决。你只需要这样实现以下两个方法:

      - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
      {
          return [UIView new];
      }
      
      - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
      {
          return CGFLOAT_MIN;
      }
      

      或者在 Swift 中

      func tableView(tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
          return CGFloat.min
      }
      
      func tableView(tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
          return UIView()
      }
      

      【讨论】:

        猜你喜欢
        • 2012-09-27
        • 2021-04-18
        • 2018-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-16
        • 2013-10-13
        相关资源
        最近更新 更多