[英]Scrolling horizontally in UITableView to see a long cell in Xcode 6 & Swift
我創建了一個tableviewcontroller並向其中添加了一個表視圖。 我需要的只是簡單地向右滑動即可查看一個長單元格(或行)。 我怎樣才能做到這一點?
只需在tableCell內添加一個簡單的UIScrollView即可,如下所示。 這將允許您在單元格中進行水平滾動。
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
/// Horizontal Scroll
var scrollview = UIScrollView(frame: cell.bounds)
scrollview.contentSize = CGSizeMake(cell.bounds.width * 5, cell.bounds.height) // will be 5 times as wide as the cell
scrollview.pagingEnabled = true
cell.contentView.addSubview(scrollview)
return cell
}
import UIKit
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource{
var tableviwe:UITableView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableviwe = UITableView(frame: self.view!.bounds)
self.tableviwe!.delegate = self
self.tableviwe!.dataSource = self;
self.tableviwe?.backgroundColor = UIColor.redColor()
let angle:CGFloat = CGFloat(M_PI_2);//angle角度 double angle;
self.tableviwe?.transform = CGAffineTransformMakeRotation(angle)
self.view?.addSubview(self.tableviwe!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//UITableViewDelegate
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let alert = UIAlertView()
alert.title = "Alert"
alert.message = NSString(format: "%@ %d", "My hit ", indexPath.row)
alert.addButtonWithTitle("Ok")
alert.show()
}
//UITableViewDataSource
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1000
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
return 200.0
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "channel")
cell.textLabel?.text = NSString(format: "%@ %d", "hello my friend", indexPath.row)
let angle:CGFloat = CGFloat(M_PI_2);//angle角度 double angle;
cell.contentView.transform = CGAffineTransformMakeRotation(-angle)
return cell
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.