如何编写一个 swift 数组,使我的所有动态单元格都显示相同的 UIImage?
How do I write a swift array that can make the same UIImage appear for all my dynamic cells?
对于我的动态原型表视图,我有 135 个单元格。所以我创建了一个变量数组:
var myLife = Array(count: 135, repeatedValue: "My Cell")
我有一个 mycell.jpg,我想在这 135 个单元格中的每一个单元格上显示,但我最终在这个数组中写了 mycell.jpg 135 次,因为我只知道这种写法图像数组。
var myCellImage = ["mycell.jpg", "mycell.jpg", "mycell.jpg", "mycell.jpg",......]
有更好的代码吗?谁能帮帮我?
您的 UITableView
中有一个名为 cellForRowAtIndexPath
的方法。每个单元格都会调用此方法。
所以,在那里你可以设置图片一次,它会为每个单元格设置:
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell = tableView.dequeueReusableCellWithIdentifier("YourCellIdentifier") as? UITableViewCell
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "YourCellIdentifier")
}
cell.imageView = UIImage(named: "mycell.jpg")
return cell
}
对于我的动态原型表视图,我有 135 个单元格。所以我创建了一个变量数组:
var myLife = Array(count: 135, repeatedValue: "My Cell")
我有一个 mycell.jpg,我想在这 135 个单元格中的每一个单元格上显示,但我最终在这个数组中写了 mycell.jpg 135 次,因为我只知道这种写法图像数组。
var myCellImage = ["mycell.jpg", "mycell.jpg", "mycell.jpg", "mycell.jpg",......]
有更好的代码吗?谁能帮帮我?
您的 UITableView
中有一个名为 cellForRowAtIndexPath
的方法。每个单元格都会调用此方法。
所以,在那里你可以设置图片一次,它会为每个单元格设置:
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell = tableView.dequeueReusableCellWithIdentifier("YourCellIdentifier") as? UITableViewCell
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "YourCellIdentifier")
}
cell.imageView = UIImage(named: "mycell.jpg")
return cell
}