gpt4 book ai didi

uitableview - 我如何使用 swift 填充我放置在 UITableCell 中的整个 UIScrollView

转载 作者:搜寻专家 更新时间:2023-11-01 05:42:05 25 4
gpt4 key购买 nike

我正在制作一个显示节日时间表的应用。该节日有多个阶段,每个阶段都有多个乐队。我的想法是为每个阶段创建一个包含行的 TableView。然后在那一行我想放置多个按钮,每个按钮代表一个乐队。我使用按钮是因为我想要一个关于乐队/艺术家的后续页面。

到目前为止我得到的是:我有一个包含 3 行的 TableView。我只用 1 个按钮填充行以检查它是否可以滚动。该按钮填充整个 ScrollView 。 scrollview 比 tablecell 长 5 倍。

我的问题是:当我滚动时,第一眼之后出现的所有内容都是空白的。而它应该被绿色按钮填充。

我的代码:

class TimeTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
//@IBOutlet var scrollView: UIScrollView!
//var imageView: UIImageView!

@IBOutlet var tableView: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count;
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

//cell.textLabel?.text = self.items[indexPath.row]

var scrollview = UIScrollView(frame: cell.bounds)
scrollview.contentSize = CGSizeMake(cell.bounds.width * 5, cell.bounds.height)
scrollview.pagingEnabled = false

var button = UIButton()

button.frame = CGRect(x: 0, y: 0, width: scrollview.frame.size.width, height: scrollview.frame.size.height)
//x: 0, y: 0, width:image.size.width, height:scrollView.frame.size.height

button.backgroundColor = UIColor.greenColor()
button.setTitle("bladiebladiebla juj een button in een scrollview in een table cell, nu nog een aantal buttons", forState: UIControlState.Normal)

//var label = UILabel(frame: scrollview.bounds)
//label.text = "bladiebladiebla juj een label in een scrollview in een table cell, nu nog een aantal buttons"

scrollview.addSubview(button)

cell.contentView.addSubview(scrollview)

return cell
}

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
println("You selected cell #\(indexPath.row)!")
}
}

最佳答案

我已经实现了您的代码,并在其中做了一些这样的更改...

开始时,Scrollview 没有正确的单元格高度,因为我在 xib 中定义为 80,但 Scrollview 高度显示在 50 左右

我做了 scrollview.removeFromSuperView 因为当我在 didSelceting 单元格之后回来时它显示正确的单元格大小 80 并将固定的滚动高度作为单元格高度..

    //change by me..
var scrollview = UIScrollView()
----------------------------------------------
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
UITableViewCell
{
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell

//cell.textLabel?.text = self.items[indexPath.row]

//change by me..
scrollview.removeFromSuperview()
scrollview = UIScrollView(frame: CGRectMake(0, 0, cell.frame.size.width, 80))
scrollview.contentSize = CGSizeMake(cell.frame.size.width * 5, cell.frame.size.height)
scrollview.pagingEnabled = false

var button = UIButton()

button.frame = CGRect(x: 0, y: 0, width: scrollview.frame.size.width, height: scrollview.frame.size.height)
//x: 0, y: 0, width:image.size.width, height:scrollView.frame.size.height

button.backgroundColor = UIColor.greenColor()
button.setTitle("bladiebladiebla juj een button in een scrollview in een table cell, nu nog een aantal buttons", forState: UIControlState.Normal)

//var label = UILabel(frame: scrollview.bounds)
//label.text = "bladiebladiebla juj een label in een scrollview in een table cell, nu nog een aantal buttons"
var button1 = UIButton()

button1.frame = CGRect(x: button.frame.size.width, y: 0, width: scrollview.frame.size.width, height: scrollview.frame.size.height)
//x: 0, y: 0, width:image.size.width, height:scrollView.frame.size.height

button1.backgroundColor = UIColor.yellowColor()
button1.setTitle("bladiebladiebla juj een button in een scrollview in een table cell, nu nog een aantal buttons", forState: UIControlState.Normal)

//var label = UILabel(frame: scrollview.bounds)
//label.text = "bladiebladiebla juj een label in een scrollview in een table cell, nu nog een aantal buttons"

scrollview.addSubview(button)
scrollview.addSubview(button1)

cell.contentView.addSubview(scrollview)
return cell

}

关于uitableview - 我如何使用 swift 填充我放置在 UITableCell 中的整个 UIScrollView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27961436/

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