swift - 如何将第一个表格 View 单元格设为静态,然后使用 swift 从第二个单元格加载数据

标签 swift uitableview cell tableviewcell

class FiorstTableViewController: UITableViewController {
    var data = ["1","2","3","4","5","6","7","8"]

    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0{
            return 1
        }
        return data.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("SecondCellfortable") as! CheckTableViewCell
        cell.LabelNumber.text = data[indexPath.row]
        if indexPath.row == 0 {
            print("my first cell")
        }
            return cell
    }
}

我需要从第二个 TableView 单元格加载数据并将第一个单元格设为静态。我已经搜索了其他堆栈溢出答案对我来说没有任何效果..帮我做这个

最佳答案

您有两种实现方式:

1) 您可以为 UITableView 创建两个单元格,第一个总是在 indexPath.row == 0 时返回 并返回您的 SecondCellfortable

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     return 50 //row count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
             let cell = tableView.dequeueReusableCellWithIdentifier("FirstCellfortable") as! CheckTableViewCellFirst
             cell.LabelNumber.text = data[indexPath.row]
             print("my first cell")
             return cell
        } else {
             tableView.dequeueReusableCellWithIdentifier("SecondCellfortable") as! CheckTableViewCellSecond
             return cell
        }
}

2) 您可以添加UITableViewHeader

func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
     let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: tableView.sectionHeaderHeight))
     view.backgroundColor = UIColor.redColor()
     // Do your customization
     return view
}

关于swift - 如何将第一个表格 View 单元格设为静态,然后使用 swift 从第二个单元格加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42510642/

相关文章:

ios - SceneKit,缩放相机以适应节点

ios - Facebook SDK swift : problems with fetching user data (segue)

ios - 如何使用 Collection View 创建 2 x 2 网格布局?

ios - 将数据追加到 UITableView 的正确方法,swift

swift - UITableView 只显示一节

iphone - 更改自定义 UITableViewCell iphone 中的文本颜色

swing - JTable : how to get selected cells?

swift - 为什么我的收藏 View 单元格颜色选择不起作用

ios - 更改从场景编辑器添加的 SKLabelNode 的文本

vba - 排序列触发单击以运行宏...如何解决此问题?