如何使第一个 table 视图单元格为静态,然后使用 swift 从第二个单元格加载数据
How to make first table view cell as static and then load data from second cell using swift
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
}
}
我需要从第二个 table 视图单元格加载数据并制作第一个单元格,因为 static.i 搜索了其他堆栈溢出答案对我来说没有任何效果..帮我做这个
同一个表格视图中不能有静态和动态单元格。我建议将您的 "first table cell" 设置为 UIView,然后将 tableheaderview 设置为您的自定义视图。
let headerView = Bundle.main.loadNibNamed("YourCustomView", owner: self, options: nil)?[0] as? YourCustomView
tableView.tableHeaderView = headerView
您有两种实现方式:
1) 您可以为 UITableView
创建两个单元格,第一个总是 return 当 indexPath.row == 0
对于所有 return 你的 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
}
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
}
}
我需要从第二个 table 视图单元格加载数据并制作第一个单元格,因为 static.i 搜索了其他堆栈溢出答案对我来说没有任何效果..帮我做这个
同一个表格视图中不能有静态和动态单元格。我建议将您的 "first table cell" 设置为 UIView,然后将 tableheaderview 设置为您的自定义视图。
let headerView = Bundle.main.loadNibNamed("YourCustomView", owner: self, options: nil)?[0] as? YourCustomView
tableView.tableHeaderView = headerView
您有两种实现方式:
1) 您可以为 UITableView
创建两个单元格,第一个总是 return 当 indexPath.row == 0
对于所有 return 你的 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
}