试图了解每个单元格如何在我的数组中显示新信息? Swift 和 IOS
Trying to understand how each cell displays new information within my array? Swift and IOS
这是我的代码的一部分。
var taskArray:[taskModel] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let task1 = taskModel (first: "Hello", second: "World")
let task2 = taskModel (first: "Hello", second: "again")
let task3 = taskModel (first: "ok", second: "we get it")
let task4 = taskModel (first: "alright", second: "we get it, you ass")
let task5 = taskModel (first: "just...", second: "Get the fruit out")
taskArray = [task1, task2, task3, task4, task5]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
println(indexPath.row)
let task = taskArray[indexPath.row]
var cell: TaskCell = tableView.dequeueReusableCellWithIdentifier("myCell") as TaskCell
cell.taskLabel.text = task.first
cell.labelLabel.text = task.second
我了解数据源和委托函数的用途,但是 "let task = taskArray[indexPath.row]" 如何在每个单元格中显示新信息?
我假设一项任务分配给一个单元格,这在我的代码中如何发挥作用?比如在哪里?
我希望有人能解释一下逻辑。
有一种叫做并行数据结构的东西。虽然这个词没有被广泛使用,也没有被广泛提及,但理解这个原理的人很多。
本质上,它是在谈论 index 与两个不同的容器匹配(UITableViewCells 和您的 data).他们可以相互匹配的方式是因为索引。它们引用同一个框(数组中的cell 和element)。这也是您需要指定数据源具有的元素数量的原因。因为另一个容器(UITableView)需要知道单元格的数量。
希望对您有所帮助。
function/method“tableView: cellForRowAtIndexPath”从您的视图控制器接收 2 个参数,以便能够定义单元格将填充的内容.在那里你有一个参数叫做“indexPath”。
如果您查看 indexPath 的构建方式,您会注意到它有一个“section” 属性 和一个“行”属性。它实际上是 table.
中单元格的地址
当您使用 taskArray[indexPath.row]
轮询数组时,您在 索引位置 轮询数组,等于 indexPath 中的单元格行 属性.
这是我的代码的一部分。
var taskArray:[taskModel] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let task1 = taskModel (first: "Hello", second: "World")
let task2 = taskModel (first: "Hello", second: "again")
let task3 = taskModel (first: "ok", second: "we get it")
let task4 = taskModel (first: "alright", second: "we get it, you ass")
let task5 = taskModel (first: "just...", second: "Get the fruit out")
taskArray = [task1, task2, task3, task4, task5]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
println(indexPath.row)
let task = taskArray[indexPath.row]
var cell: TaskCell = tableView.dequeueReusableCellWithIdentifier("myCell") as TaskCell
cell.taskLabel.text = task.first
cell.labelLabel.text = task.second
我了解数据源和委托函数的用途,但是 "let task = taskArray[indexPath.row]" 如何在每个单元格中显示新信息?
我假设一项任务分配给一个单元格,这在我的代码中如何发挥作用?比如在哪里?
我希望有人能解释一下逻辑。
有一种叫做并行数据结构的东西。虽然这个词没有被广泛使用,也没有被广泛提及,但理解这个原理的人很多。
本质上,它是在谈论 index 与两个不同的容器匹配(UITableViewCells 和您的 data).他们可以相互匹配的方式是因为索引。它们引用同一个框(数组中的cell 和element)。这也是您需要指定数据源具有的元素数量的原因。因为另一个容器(UITableView)需要知道单元格的数量。
希望对您有所帮助。
function/method“tableView: cellForRowAtIndexPath”从您的视图控制器接收 2 个参数,以便能够定义单元格将填充的内容.在那里你有一个参数叫做“indexPath”。
如果您查看 indexPath 的构建方式,您会注意到它有一个“section” 属性 和一个“行”属性。它实际上是 table.
中单元格的地址当您使用 taskArray[indexPath.row]
轮询数组时,您在 索引位置 轮询数组,等于 indexPath 中的单元格行 属性.