【发布时间】:2020-12-06 01:03:29
【问题描述】:
我希望我的 swift 代码附加在 tableview 单元格的每个标签中显示的所有整数。所以在数组中它应该包含 0,1,2。该数组被声明为 someArray 并带有一个 int 值。 func 视图中的附加确实加载了。我不知道用 append 把它放在括号内。我不知道还要添加什么代码
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var numberOfRows = 3
var someArray = [Int]()
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { numberOfRows }
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 118 }
var tableView = UITableView()
var selectedIndexPath = IndexPath(row: 0, section: 0)
override func viewDidLoad() {
super.viewDidLoad()
setTableVIew()
someArray.append(c)
}
func setTableVIew(){
let VCframe = view.frame
let height = VCframe.height * 0.8
let widthx = VCframe.width
tableView.frame = CGRect(x: 10, y: 0, width: widthx - 20, height: height)
tableView.delegate = self
tableView.dataSource = self
view.addSubview(tableView)
tableView.register(customtv.self, forCellReuseIdentifier: "cell")
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! customtv
cell.lbl.text = "\(indexPath.row)"
return cell
}
}
class customtv: UITableViewCell {
lazy var backView : UIView = {
let view = UIView(frame: CGRect(x: 10, y: 6, width: self.frame.width , height: 110))
view.backgroundColor = .green
print(self.frame.width)
return view
}()
override func layoutSubviews() {
backView.clipsToBounds = true
backView.frame = CGRect(x: 0, y: 6, width: bounds.maxX , height: 110)
}
lazy var lbl : UILabel = {
let press = UILabel(frame: CGRect(x: 0, y: 3, width: 120 , height: 50))
press.backgroundColor = .yellow
press.text = String("1")
return press
}()
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(animated, animated: true)
addSubview(backView)
backView.addSubview(lbl)
}
}
【问题讨论】:
-
你的意思是
var someArray = [0, 1, 2]? -
@JoakimDanielson 不,我希望数组将表中的值附加到该数组中。表格视图中的数字将是 0 1 2
-
但是,如果这些值始终为 0、1、2,那么您如何使用它们初始化/附加数组又有什么关系呢?
-
@JoakimDanielson 这些值并不总是 012,它们只是 tableview 标签单元格上的前 3 个数字。我只想从表格视图单元格中获取前 3 个数字。
-
那些值一定来自某个地方,为什么不从源头获取呢?
标签: arrays swift uitableview append