gpt4 book ai didi

Swift: ScrollView 中的两个tableView

转载 作者:行者123 更新时间:2023-11-30 12:58:46 27 4
gpt4 key购买 nike

我使用 Xcode 8.0 和 swift 2.3。

我有一个scrollView,我想用segment来控制页面

http://imgur.com/a/KZFIS

scrollView中有两个tableView

但是函数中出现错误:

   func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

Contstant 'cell' used before being initialized

这是错误

http://imgur.com/a/nooVG

这是初始化“cell”,对吧?

 let cell:UITableViewCell?

上面是我的代码

class DetailViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate  {
@IBOutlet weak var scroll: UIScrollView!
@IBOutlet weak var segmentControl2: UISegmentedControl!

@IBOutlet var recipesImageView: UIImageView!
@IBOutlet var tableView:UITableView!
@IBOutlet var tableView1:UITableView!
var recipes:Recipe!
var noodles:Recipe!
var snacks:Recipe!
var category: Int = Int()

@IBAction func segmented(sender: AnyObject) {
switch sender.selectedSegmentIndex {
case 0:
scroll.setContentOffset(CGPoint(x:0, y:0), animated: true)
case 1:
scroll.setContentOffset(CGPoint(x:375, y:0), animated: true)
case 2:
scroll.setContentOffset(CGPoint(x:750, y:0), animated: true)
case 3:
scroll.setContentOffset(CGPoint(x:1125, y:0), animated: true)


default:
print()
}
}

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
switch self.category {
case 0:
self.recipesImageView.image = UIImage(named: recipes.image)
title = self.recipes.name
case 1:
self.recipesImageView.image = UIImage(named: noodles.image)
title = self.noodles.name
case 2:
self.recipesImageView.image = UIImage(named: snacks.image)
title = self.snacks.name
case 3:
self.recipesImageView.image = UIImage(named: noodles.image)
title = self.noodles.name
case 4:
self.recipesImageView.image = UIImage(named: noodles.image)
title = self.noodles.name
default:
self.recipesImageView.image = UIImage(named: recipes.image)
title = self.recipes.name
}

self.tableView.backgroundColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 0.5)

//刪除多餘的隔線
self.tableView.tableFooterView = UIView(frame: CGRectZero)

self.tableView.separatorColor = UIColor(red: 240.0/255.0, green: 240.0/255.0, blue: 240.0/255.0, alpha: 1)

//title = self.recipes.name

tableView.estimatedRowHeight = 36.0;
tableView.rowHeight = UITableViewAutomaticDimension;

tableView.dataSource = self
tableView.delegate = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell")

tableView1.dataSource = self
tableView1.delegate = self
tableView1.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell1")

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var cellNum:Int?
if tableView == self.tableView {
cellNum = 4
//return cellNum
}

if tableView == self.tableView1 {
cellNum = 1
}
return cellNum!
}



func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell:UITableViewCell?

if tableView == self.tableView {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! DetailTableViewCell
//cell.backgroundColor = UIColor.clearColor()
if self.category == 0 {
switch indexPath.row {
case 0:
cell.fieldLabel.text = "名稱"
cell.valueLabel.text = recipes.name
cell.amountLabel.text = ""

case 1:
cell.fieldLabel.text = "類型"
cell.valueLabel.text = recipes.type
cell.amountLabel.text = ""

case 2:
cell.fieldLabel.text = "收藏"
cell.valueLabel.text = (recipes.isFavor) ? "Yes" : "No"
cell.amountLabel.text = ""

case 3:
cell.fieldLabel.text = "食材"
cell.valueLabel.text = recipes.material
cell.amountLabel.text = recipes.amount
default:
cell.fieldLabel.text = ""
cell.valueLabel.text = ""
cell.amountLabel.text = ""
}
}

if self.category == 1 {
switch indexPath.row {
case 0:
cell.fieldLabel.text = "名稱"
cell.valueLabel.text = noodles.name
cell.amountLabel.text = ""

case 1:
cell.fieldLabel.text = "類型"
cell.valueLabel.text = noodles.type
cell.amountLabel.text = ""

case 2:
cell.fieldLabel.text = "收藏"
cell.valueLabel.text = (noodles.isFavor) ? "Yes" : "No"
cell.amountLabel.text = ""

case 3:
cell.fieldLabel.text = "食材"
cell.valueLabel.text = noodles.material
cell.amountLabel.text = noodles.amount
default:
cell.fieldLabel.text = ""
cell.valueLabel.text = ""
cell.amountLabel.text = ""
}
}

if self.category == 2 {
switch indexPath.row {
case 0:
cell.fieldLabel.text = "名稱"
cell.valueLabel.text = snacks.name
cell.amountLabel.text = ""

case 1:
cell.fieldLabel.text = "類型"
cell.valueLabel.text = snacks.type
cell.amountLabel.text = ""

case 2:
cell.fieldLabel.text = "收藏"
cell.valueLabel.text = (snacks.isFavor) ? "Yes" : "No"
cell.amountLabel.text = ""

case 3:
cell.fieldLabel.text = "食材"
cell.valueLabel.text = snacks.material
cell.amountLabel.text = snacks.amount
default:
cell.fieldLabel.text = ""
cell.valueLabel.text = ""
cell.amountLabel.text = ""
}
}

if self.category == 3 {
switch indexPath.row {
case 0:
cell.fieldLabel.text = "名稱"
cell.valueLabel.text = noodles.name
cell.amountLabel.text = ""

case 1:
cell.fieldLabel.text = "類型"
cell.valueLabel.text = noodles.type
cell.amountLabel.text = ""

case 2:
cell.fieldLabel.text = "收藏"
cell.valueLabel.text = (noodles.isFavor) ? "Yes" : "No"
cell.amountLabel.text = ""

case 3:
cell.fieldLabel.text = "食材"
cell.valueLabel.text = noodles.material
cell.amountLabel.text = noodles.amount
default:
cell.fieldLabel.text = ""
cell.valueLabel.text = ""
cell.amountLabel.text = ""
}
}

if self.category == 4 {
switch indexPath.row {
case 0:
cell.fieldLabel.text = "名稱"
cell.valueLabel.text = noodles.name
cell.amountLabel.text = ""

case 1:
cell.fieldLabel.text = "類型"
cell.valueLabel.text = noodles.type
cell.amountLabel.text = ""

case 2:
cell.fieldLabel.text = "收藏"
cell.valueLabel.text = (noodles.isFavor) ? "Yes" : "No"
cell.amountLabel.text = ""

case 3:
cell.fieldLabel.text = "食材"
cell.valueLabel.text = noodles.material
cell.amountLabel.text = noodles.amount
default:
cell.fieldLabel.text = ""
cell.valueLabel.text = ""
cell.amountLabel.text = ""
}
}
//return cell
}

if tableView == self.tableView1 {
let cell = tableView1.dequeueReusableCellWithIdentifier("Cell1", forIndexPath: indexPath) as! DetailTableViewCell

if self.category == 0 {
switch indexPath.row {
case 0:
cell.materialLabel!.text = recipes.material
default:
cell.materialLabel!.text = recipes.material
}
}
if self.category == 1 {
switch indexPath.row {
case 0:
cell.materialLabel!.text = noodles.material
default:
cell.materialLabel!.text = noodles.material
}
}
if self.category == 2 {
switch indexPath.row {
case 0:
cell.materialLabel!.text = snacks.material
default:
cell.materialLabel!.text = snacks.material
}
}
if self.category == 3 {
switch indexPath.row {
case 0:
cell.materialLabel!.text = noodles.material
default:
cell.materialLabel!.text = noodles.material
}
}
if self.category == 4 {
switch indexPath.row {
case 0:
cell.materialLabel!.text = noodles.material
default:
cell.materialLabel!.text = noodles.material
}
}
}

return cell //<----This line will error ***********************
}


//避免被前面一頁的隱藏導覽列所影響
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)

self.navigationController?.hidesBarsOnSwipe = false
self.navigationController?.setNavigationBarHidden(false, animated: true)
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/

}

最佳答案

我相信这些改变应该有所帮助:

  1. let cell:UITableViewCell? 更改为 let cell:UITableViewCell。该函数不希望您返回可选值。

  2. let cell = tableView.dequeue... 更改为 cell = tableView.dequeue...。您不应声明 cell 两次。

  3. cellForRowAtIndexPath 中的结构更新为:

if tableView == self.tableView {
细胞=...
...
} 别的 {
细胞=...
...
}
返回单元格

因此编译器会发现您总是在返回单元格之前将其设置为某些内容。

关于Swift: ScrollView 中的两个tableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40099307/

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