gpt4 book ai didi

ios - 在 TableView 中仅显示第一个单元格

转载 作者:行者123 更新时间:2023-11-28 15:00:45 25 4
gpt4 key购买 nike

我在 View Controller 的 map View 之上使用了 TableView 和 TableView 单元格,我只想显示第一个单元格。我已尝试将 numberOfRowsInSection 设置为 1,这会使我的应用程序崩溃。

填充单元格和确定节中行数的数据来自核心数据。我已经成功地只显示了我想要的数据,它只是重复了一堆而不是只显示一个单元格。

我想要这个,就像我在单独的 TableView Controller 中一样:

enter image description here

相反,我得到了这个:

enter image description here

我的 cellForRowAt:

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

// Configure cell
let cell = tableView.dequeueReusableCell(withIdentifier: "MapTableViewCell") as! MapTableViewCell
cell.thumbnailImageView.layer.cornerRadius = 10
cell.horizontalStackView.addBackground(color: UIColor.white)

// Get data
var cellLocation: Location

for location in locations {
if location.latitude == annotation.coordinate.latitude && location.longitude == annotation.coordinate.longitude {
cellLocation = location

YelpClient.sharedInstance().loadImage(cellLocation.imageUrl, completionHandler: { (image) in

performUIUpdatesOnMain {

cell.thumbnailImageView.layer.cornerRadius = 10
cell.thumbnailImageView.clipsToBounds = true
cell.thumbnailImageView.image = image
cell.nameLabel.text = cellLocation.name
cell.priceLabel.text = cellLocation.price
cell.displayRating(location: cellLocation)
}

YelpClient.sharedInstance().getOpeningHoursFromID(id: cellLocation.id, completionHandlerForOpeningHours: { (isOpenNow, error) in

if let error = error {
print("There was an error: \(String(describing: error))")
}

if let isOpenNow = isOpenNow {

performUIUpdatesOnMain {

if isOpenNow {
cell.openLabel.text = "Open"
cell.openLabel.textColor = UIColor.black
} else {
cell.openLabel.text = "Closed"
cell.openLabel.textColor = UIColor(red: 195/255, green: 89/255, blue: 75/255, alpha: 1.0)
cell.openLabel.font = UIFont.systemFont(ofSize: 17.0, weight: .semibold)
}
}
}
})
})
}
}
return cell
}

编号的行数:

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return locations.count
}

让我知道我错过了什么。

最佳答案

创建一个只接收你想要的数据的类变量,即使它只是一个项目:

var location1Item = [location]()
locations1Item.append(locations[0]) //Or the item you want to show

现在使用 location1Item 填充 tableView

编号的行数:

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return location1Item.count
}

cellForRowAt:

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

// Configure cell
let cell = tableView.dequeueReusableCell(withIdentifier: "MapTableViewCell") as! MapTableViewCell
cell.thumbnailImageView.layer.cornerRadius = 10
cell.horizontalStackView.addBackground(color: UIColor.white)

// Get data
var cellLocation: Location

for location in location1Item {
if location.latitude == annotation.coordinate.latitude && location.longitude == annotation.coordinate.longitude {
cellLocation = location

YelpClient.sharedInstance().loadImage(cellLocation.imageUrl, completionHandler: { (image) in

performUIUpdatesOnMain {

cell.thumbnailImageView.layer.cornerRadius = 10
cell.thumbnailImageView.clipsToBounds = true
cell.thumbnailImageView.image = image
cell.nameLabel.text = cellLocation.name
cell.priceLabel.text = cellLocation.price
cell.displayRating(location: cellLocation)
}

YelpClient.sharedInstance().getOpeningHoursFromID(id: cellLocation.id, completionHandlerForOpeningHours: { (isOpenNow, error) in

if let error = error {
print("There was an error: \(String(describing: error))")
}

if let isOpenNow = isOpenNow {

performUIUpdatesOnMain {

if isOpenNow {
cell.openLabel.text = "Open"
cell.openLabel.textColor = UIColor.black
} else {
cell.openLabel.text = "Closed"
cell.openLabel.textColor = UIColor(red: 195/255, green: 89/255, blue: 75/255, alpha: 1.0)
cell.openLabel.font = UIFont.systemFont(ofSize: 17.0, weight: .semibold)
}
}
}
})
})
}
}
return cell
}

关于ios - 在 TableView 中仅显示第一个单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48956387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com