gpt4 book ai didi

swift - 我想在 swift 4 上将三个数组放在一个数组中( TableView )

转载 作者:可可西里 更新时间:2023-11-01 00:19:07 24 4
gpt4 key购买 nike

我想把所有的数组放在一个数组中,如果可以的话,我想把变化的图像代码放在一个数组中

let priceData = ["$7.99", "$4.59", "$2.29", "$1.19", "$2.29" , "$1.19"]
let appNAme = ["Minecraft: Pocket Edition","Enlight", "Geometry Dash", "Plague Inc.", "R.B.I. Baseball 15", "Heads Up!"]
let reviewsNumberData = ["(167)", "(71)", "(975)", "(629)", "(12)", "(5)"]


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = myTableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCellTableViewCell

// Setting Revies Number Label

cell.reviewsNumberLabel.text = reviewsNumberData[indexPath.row]

// Setting In-App Purchases label

// Hide In-App Purchases in Row 1 ---
if(indexPath.row == 1) {
cell.inappLabel.isHidden = true
}
// Hide In-App Purchases in Row 2 ---
if(indexPath.row == 2) {
cell.inappLabel.isHidden = true
}

// Hide In-App Purchases in Row 4 ---
if(indexPath.row == 4) {
cell.inappLabel.isHidden = true
}
// Setting Counter for Apps

cell.appCount.text = "\(indexPath.row + 1)"

// Setting Type of App

if(indexPath.row == 1){
cell.appTypeLabel.text = "Photo & Video"
}

// Setting App Name

cell.appNameLabel.text = appNAme[indexPath.row]

// Setting Price Button

cell.priceButton.titleLabel?.text = "ddd"
cell.priceButton.setTitle(priceData[indexPath.row], for: .normal)

// Setting Image Change

// Enlight
if(indexPath.row == 1){
cell.appImage.image = UIImage(named: "Enlight")
}

// Geometry Dash
if(indexPath.row == 2){
cell.appImage.image = UIImage(named: "GeometryDash")
}

// Plague Inc
if(indexPath.row == 3){
cell.appImage.image = UIImage(named: "plague")
}
// Rbi Baseball
if(indexPath.row == 4){
cell.appImage.image = UIImage(named: "Baseball")
}
// Heads Up !
if(indexPath.row == 5){
cell.appImage.image = UIImage(named: "HeadsUp")
}


return cell

最佳答案

创建具有属性的结构

struct Item {
var price: String
var name: String
var reviews: String
var image: UIImage
var inappLabelShow = false
}

并创建一个Item数组

var items = [Item]()

在tableview数据源方法中使用这个数组

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return items.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

let cell = myTableView.dequeueReusableCell(withIdentifier: "MyCell") as! MyCellTableViewCell

cell.reviewsNumberLabel.text = items[indexPath.row].reviews
cell.appImage.image = items[indexPath.row].image
cell.appNameLabel.text = items[indexPath.row].name
cell.priceButton.setTitle(items[indexPath.row].price, for: .normal)
cell.inappLabel.isHidden = items[indexPath.row].inappLabelShow
return cell
}

您可以通过枚举数组对象来创建结构对象

let item1 = Item(price: "$7.99", name: "Minecraft: Pocket Edition", reviews: "(167)", image: nil)
items.append(item1)
let item2 = Item(price: "$4.59", name: "Enlight", reviews: "(71)", image: UIImage(named: "Enlight"), inappLabelShow: true)
items.append(item2)

关于swift - 我想在 swift 4 上将三个数组放在一个数组中( TableView ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56243683/

24 4 0