gpt4 book ai didi

ios - UiTableView 生成的单元格数量

转载 作者:行者123 更新时间:2023-11-30 12:03:48 24 4
gpt4 key购买 nike

我有两个 View Controller 。第一个带有步进器:

var steppers : UIStepper?

@IBOutlet weak var hourLabel: UILabel!
@IBOutlet weak var stepper: UIStepper!

@IBAction func stepper(_ sender: UIStepper) {
hourLabel.text = String(sender.value)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let np = segue.destination as? CourseClass2 {
np.numberPlaces = Int(stepper.value)
}
}

第二个带有 tableView:

private let resueIdentifier = "MyTableViewCell"

extension UIViewController {
func present(viewController : UIViewController, completion : (() -> ())? = nil ){
if let presented = self.presentedViewController {
presented.dismiss(animated: true, completion: {
self.present(viewController, animated: true, completion: completion)
})
} else {
self.present(viewController, animated: true, completion: completion)
}
}
}

class CourseClass2: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!

var locationManager:CLLocationManager?
let minimumSpacing : CGFloat = 15 //CGFloat(MAXFLOAT)
let cellWidth: CGFloat = 250
let radius = 5000 // 5km
var category : QCategoryy?
var currentLocation : CLLocationCoordinate2D?
var places: [QPlace] = []
var isLoading = false
var response : QNearbyPlacesResponse?
var rows = 0
var numberPlaces = 0

override func viewDidLoad() {
super.viewDidLoad()
self.title = category?.name
tableView.dataSource = self
tableView.delegate = self
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

determineMyCurrentLocation()
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

rows = 0
/* insertRowsMode3()
tableView.reloadData() */
category?.markView()
}

@IBAction func refreshTapped(_ sender: Any) {
rows = 0
/* insertRowsMode3() */
tableView.reloadData()
}

func canLoadMore() -> Bool {
if isLoading {
return false
}

if let response = self.response {
if (!response.canLoadMore()) {
return false
}
}

return true
}

func loadPlaces(_ force:Bool) {
if !force {
if !canLoadMore() {
return
}
}

print("load more")
isLoading = true
NearbyPlaces.getNearbyPlaces(by: category?.name ?? "food", coordinates: currentLocation!, radius: radius, token: self.response?.nextPageToken, completion: didReceiveResponse)
}

func didReceiveResponse(response:QNearbyPlacesResponse?, error : Error?) -> Void {
if let error = error {
let alertController = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
let actionDismiss = UIAlertAction(title: "Dismiss", style: .cancel, handler: nil)
let actionRetry = UIAlertAction(title: "Retry", style: .default, handler: { (action) in
DispatchQueue.main.async {
self.loadPlaces(true)
}
})
alertController.addAction(actionRetry)
alertController.addAction(actionDismiss)
DispatchQueue.main.async {
self.present(viewController: alertController)

}
}
if let response = response {
self.response = response
if response.status == "OK" {
if let placesDownloaded = response.places {
places.append(contentsOf: placesDownloaded)
}

self.tableView?.reloadData()
} else {
let alert = UIAlertController.init(title: "Error", message: response.status, preferredStyle: .alert)
alert.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
alert.addAction(UIAlertAction.init(title: "Retry", style: .default, handler: { (action) in
DispatchQueue.main.async {
self.loadPlaces(true)
}
}))
self.present(viewController: alert)
}
isLoading = false
}
else {
print("response is nil")
}
}

func numberOfSections(in tableView: UITableView) -> Int {
print("numberOfsection Call")
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("numberOfRows Call")
return places.count /* rows */
}

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: resueIdentifier, for: indexPath) as! MyTableViewCell

let place = places[indexPath.row]
cell.update(place: place)

if indexPath.row == places.count - 1 {
loadPlaces(false)
}
print("CellForRow Call")
return (cell)
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)

UIView.animate(withDuration: 0.2, animations: {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyTableViewCell", for: indexPath) as! MyTableViewCell
})

performSegue(withIdentifier: "goToLast" , sender: indexPath.row)
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == UITableViewCellEditingStyle.delete {
places.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}

func didReceiveUserLocation(_ userLocation:CLLocation) {
currentLocation = userLocation.coordinate

loadPlaces(true)
}

我只添加了对问题有用的代码。正如你所看到的,我试图将这个值从第一个 VC 传递到第二个:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let np = segue.destination as? CourseClass2 {
np.numberPlaces = Int(stepper.value)
}
}

因为我想对在第二个 viewController 中加载 tableview 时生成的单元格数量添加限制(并且我希望用户使用前一个 Controller 中的步进器选择此限制),我该如何是否必须修改 tableView 以确保生成的单元格数量等于 var numberPlaces 的值?

最佳答案

我建议您限制UITableView委托(delegate)方法单元格数量

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("numberOfRows Call")
if places.count < self.numberPlaces {
return places.count /* rows */
}
return self.numberPlaces
}

通过这种方式,您将获得正确数量的单元格,但如果位置太多,则仅显示 self.numberPlaces 。

也许,您需要进行一些排序,以便显示正确的位置。 (根据距离,...)

关于ios - UiTableView 生成的单元格数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46891764/

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