2016-05-31 65 views
0

我正在尝试使用滑块和表格视图在Swift中制作简单的时间表应用(用于数字1-9)。我正在设法使滑块工作,并为每个使用滑块选择的数字创建一个数组,并且该数组显示在控制台上。我无法让数字出现在表格视图中。你能帮我,告诉我我错过了什么吗?无法在简单的时间表中显示表格视图数据应用

这是我到目前为止写:

class ViewController: UIViewController, UITableViewDelegate { 

    @IBOutlet var sliderValue: UISlider! 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 9 
    } 

    @IBAction func sliderMoved(sender: UISlider) { 
     sender.setValue(Float(lroundf(sliderValue.value)), animated: true) 

     print(sliderValue) 

     var cellContent = [String]() 

     for var i = 1; i <= 10; i += 1 { 
      cellContent.append(String(i * Int(sliderValue.value))) 
     } 

     print(cellContent) 

     func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
      let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell") 

      cell.textLabel?.text = cellContent[indexPath.row] 

      return cell 
     } 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
    } 

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

你提供的实际上不涉及任何的tableView也不成立的tableView的委托和数据源代码。此外,你的类坚持'UITableViewDelegate'协议,但实际上实现了几个'UITableViewDataSource'函数,这可能不会有太大的用处... –

回答

1

恐怕有不少在您提供的代码,不会使所有的东西的感觉。我在上面的评论中提到了它的一些,但是你也将类似tableViewDataSource-函数的东西嵌套到了sliderMoved函数中。整个数组的东西看起来相当飘忽不定,所提出的单元数实际上并不考虑数组的大小。我想你可能想是这样的:

class ViewController: UIViewController, UITableViewDataSource { 
    @IBOutlet var valueSlider: UISlider! 
    @IBOutlet var tableView: UITableView! 
    private var cellContent = [String]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     tableView.dataSource = self 
    } 

    @IBAction func sliderMoved(sender: UISlider) { 
     sender.setValue(Float(lroundf(valueSlider.value)), animated: true) 
     tableView.reloadData() 
    } 

    // TableViewDataSource 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return 9 
    } 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell") // Must exist with the same identifier in your storyboard 
     cell.textLabel?.text = valueStringForIndex(indexPath.row) 

     return cell 
    } 

    // Private functions 

    private func valueStringForIndex(index: Int) -> String { 
     return "\(index * Int(valueSlider.value))" 
    } 
} 
+0

首先我要感谢您的快速回复。好吧,我几乎可以肯定,我可能会写错的东西,因为我最近开始快速学习,而我正在做的是试图将我正在从网上学习的课程中学到的东西应用于我自己在同一时间几件事情。也就是说,我知道我在其他方法中嵌套了这种方法,但我只是试图使代码有效,并且在我发布该问题的那一刻就被认为是这样。非常感谢你的回答,它正在工作,尽管我仍然试图理解几件事情的工作原理。 – Ioannis

1

您是否尝试过创建cellContent数组作为实例变量和下面的代码可能会工作。检查一次。

var cellContent = [String]() 
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    return 9 
} 

@IBAction func sliderMoved(sender: UISlider) { 
    sender.setValue(Float(lroundf(sliderValue.value)), animated: true) 

    print(sliderValue) 



    for var i = 1; i <= 10; i += 1 { 
     cellContent.append(String(i * Int(sliderValue.value))) 
    } 

    print(cellContent) 

    self.tableview.reloadData() 
} 
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
     let cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "Cell") 

     cell.textLabel?.text = cellContent[indexPath.row] 

     return cell 
    } 
override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 

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

}

+0

我已经想到了这一点,并试图在发布我的问题之前(它也是一个来自Xcode的推荐)。但实际上,一旦我尝试运行代码并使应用程序崩溃,它就会造成更大的问题。不管怎么说,还是要谢谢你! – Ioannis

0

虽然没有直接回答你的问题 - >

根据您想要的表中显示,一个UICollectionView可能是一个很好的契合应用。非常类似于UITableView的实现,但带有框和数据列,格式更简单(并且更改滑块可以在收集视图更新时添加一些有趣的动画)。

下面的示例UIViewController演示了如何使用UICollectionView。在故事板,我简单:

  • 增加了一个UISlider,UICollectionView和的UILabel和MultiplicationTableViewController
  • 创建网点在我给它的reuseIdentifier“numberCell”,并增加了一个标签UICollectionView默认的单元格(持有产品)
  • 作出MultiplicationTableViewController的数据源和委托的UICollectionView

CODE:

import UIKit 

class MultiplicationTableViewController: UIViewController { 

    @IBOutlet var timesTableCollectionView: UICollectionView! 
    @IBOutlet weak var numberSlider: UISlider! 
    @IBOutlet weak var label: UILabel! 

    var products = [Int]() //array to hold the computed value for each cell in the collectionView 

    override func viewDidLoad() { 
    super.viewDidLoad() 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.viewRotated), name: UIDeviceOrientationDidChangeNotification, object: nil) //register for rotation notifications 
    products = createTableOfValues()    //populate products with initial values 
    label.text = "\(Int(numberSlider.value)) x \(Int(numberSlider.value))" 
    } 

    @IBAction func sliderUpdated(sender: UISlider) { 
    sender.value = Float(Int(sender.value))  //make the slider stop only on whole numbers 
    label.text = "\(Int(sender.value)) x \(Int(sender.value))" 
    products = createTableOfValues()    //create the new table values 
    timesTableCollectionView.reloadData()  //tell the collectionView to read the new data and refresh itself 
    } 

    func createTableOfValues() -> [Int] { 
    var prod = [Int]()       //temp array to hold the generated products 
    for row in 0...Int(numberSlider.value) {  //iterate from row 0 (header) to 
     var columns = [Int]()      //temp array to build column products 
     for column in 0...Int(numberSlider.value) {//iterate through each column, including column 0 (header) 
     if column == 0 { 
      columns.append(row) 
     } else if row == 0 { 
      columns.append(column) 
     } else { 
      columns.append(column * row) 
     } 
     } 
     prod.appendContentsOf(columns)    //add the current row of products to the temp array 
    } 
    return prod 
    } 

    func viewRotated() { 
    timesTableCollectionView.reloadData()  //called to force collectionView to recalc (basically to get new cell sizes 
    } 
} 

extension MultiplicationTableViewController: UICollectionViewDataSource { 

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { 
    return 1         //required for UICollectionViewDataSource 
    } 

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return Int(numberSlider.value + 1) * Int(numberSlider.value + 1) //tells the UICollectionView how many cells to draw (the number on the slider, plus header rows) 
    } 

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("numberCell", forIndexPath: indexPath) //get an existing cell if it exists 
    if cell.frame.origin.y == 0.0 || cell.frame.origin.x == 0.0 {  //if the cell is at the top or left of the collectionView 
     cell.backgroundColor = UIColor.yellowColor() 
    } else { 
     cell.backgroundColor = UIColor.clearColor()      //If not, reset the color (required because cells are reused 
    } 
    cell.layer.borderColor = UIColor.blackColor().CGColor 
    cell.layer.borderWidth = 1.0 
    let numberItem = cell.viewWithTag(101) as? UILabel    //get a reference to the label in the current cell 
    numberItem?.text = String(products[indexPath.row])    //get the value generated earlier for this particular cell 
    return cell 
    } 
} 

extension MultiplicationTableViewController: UICollectionViewDelegateFlowLayout { 
    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
    let columns = CGFloat(numberSlider.value + 1)      //get the number of columns - slider value + 1 for header 
    let width = timesTableCollectionView.bounds.width/columns   //divide the width of the collectionView by the number of columns 
    return CGSizeMake(width, width)          //use width value to make the cell a square 
    } 
} 

截图: enter image description here

+0

正如我在另一条评论中提到的那样,我对swift和制作ios应用程序很陌生,所以每一种新的和/或不同的制作方式总是很有趣。 我一定希望看到它如何以不同的方式实现,因此,欢迎您提供代码或资源或任何您认为会有所帮助的东西。先谢谢你! – Ioannis

+0

如果您想进一步观察,我会在稍后将项目发布到gitHub。 – DJohnson

相关问题