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
}
}
我需要从第二个表格视图单元格中加载数据,并将第一个单元格设置为static.i已经搜索了其他堆栈溢出答案,对我来说没有任何效果。帮我做一个
答案 0 :(得分:3)
您有两种方法可以实现:
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
}
答案 1 :(得分:0)
您不能在同一个tableview中拥有静态和动态单元格。我建议您设置第一个表格单元格"作为UIView然后将tableheaderview设置为您的自定义视图。
let headerView = Bundle.main.loadNibNamed("YourCustomView", owner: self, options: nil)?[0] as? YourCustomView
tableView.tableHeaderView = headerView