Spritekit - Tableview滚动切断

问题描述:

我目前正在开发一个菜单屏幕,在我的精灵套件游戏中显示所有项目,并且我使用了tableview来实现这个目的,因为它允许我为项目描述提供一个uilabel 。 我的UITableView的子类如下:Spritekit - Tableview滚动切断

的UITableView类

import Foundation 
import SpriteKit 
import UIKit 

class GameRoomTableView: UITableView,UITableViewDelegate,UITableViewDataSource { 

var items: [String] = [] 
var descrip: [String] = [] 
var title: [String] = [] 

var isFirstViewAlreadyAdded = false 
var isSecondViewAlreadyAdded = false 

override init(frame: CGRect, style: UITableViewStyle) { 
    super.init(frame: frame, style: style) 
    self.delegate = self 
    self.dataSource = self 
    coreDataItemRetrieveval() 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 

// MARK: - Table view data source 
func numberOfSections(in tableView: UITableView) -> Int { 

    return items.count 
} 

func coreDataItemRetrieveval() { 

    items.removeAll(); descrip.removeAll(); title.removeAll() 

    items.append("Player1") 
    descrip.append("Grown on Astrums Home world of Zaharia the forbidden fruit when eaten results in a headstart for the player") 
    title.append("Athia Fruit") 

    items.append("Player2") 
    descrip.append("HHHHHH HHHHHH HHHHHHHHHH HHHHHHH HHHHHHHH HHHHHHHH HHHHHHHHH HHHHHHHHH ") 
    title.append("AtTTTTTTT") 

    items.append("Player2") 
    descrip.append("TESTING ") 
    title.append("AtTTTTTTT") 

    items.append("Player2") 
    descrip.append("TESTING ") 
    title.append("AtTTTTTTT") 

    items.append("Player2") 
    descrip.append("TESTING ") 
    title.append("AtTTTTTTT") 

} 

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    tableView.allowsSelection = false 
    tableView.showsVerticalScrollIndicator = false 
    tableView.backgroundColor = .clear 
    tableView.backgroundView = nil 

    return 1 
} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let CellIdentifier: String = "Cell" 
    var cell: UITableViewCell? = tableView.dequeueReusableCell(withIdentifier: CellIdentifier) 

     if cell == nil { 

      cell = UITableViewCell(style: .default, reuseIdentifier: nil) 

      //IMPLEMENT CORE DATA RETRIEVEAL AND SO ON TO MAKE IT BETTER USE APPEND ARRAYS AND SO ON TO GET THIS DONE AND IMPLEMENT QUANTITY LABEL. 

      cell?.imageView?.image = UIImage(named:(self.items[indexPath.section] + ".png")) 
      cell?.imageView?.transform = CGAffineTransform(scaleX: 0.5, y: 0.5); 

      cell?.textLabel?.text = self.descrip[indexPath.section] 
      cell?.textLabel?.numberOfLines = 0 
      cell?.textLabel?.adjustsFontSizeToFitWidth = true 
      cell?.textLabel?.frame = CGRect(x: (cell?.frame.size.width)!/2.6, y: (cell?.frame.size.height)!/1.7, width: 150, height: 50) 

      let textlabel2 = UILabel(frame: CGRect(x: (cell?.frame.size.width)!/2.6, y: (cell?.frame.size.height)!/1.4, width: 150, height: 50)) 
      textlabel2.text = self.title[indexPath.section] 
      textlabel2.numberOfLines = 0 
      textlabel2.adjustsFontSizeToFitWidth = true 
      cell?.contentView.addSubview(textlabel2) 

     } 

     return cell! 
} 

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

func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int){ 

    view.tintColor = UIColor.clear 
    let header = view as! UITableViewHeaderFooterView 
    header.textLabel?.textColor = UIColor.white 
} 

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return " " 
} 

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
    print("You selected cell #\(indexPath.row)!") 
    } 

} 

GameScene:

class GameScene: SKScene { 

var gameTableView = GameRoomTableView() 
private var label : SKLabelNode? 

override func didMove(to view: SKView) { 

    gameTableView.frame = CGRect(x:14,y:100, width: frame.maxX/1.08, height: frame.maxY) //(scene?.view?.frame.maxY)!) 
    gameTableView.contentSize = CGSize(width: gameTableView.frame.size.width, height: gameTableView.frame.size.height) 

    self.scene?.view?.addSubview(gameTableView) 
    gameTableView.reloadData() 

    } 
    } 

我唯一的问题是,当我滚动到的tableview的底部,它似乎最后一个单元格的一半被切断滚动到,我不能看到它完全tableview有多个部分,因为我想间隙每个细胞之间,这是我能实现它的唯一途径。如何将tableview的滚动更改为更长,以便我可以完全看到所有单元格?我试图在这里找到其他答案,我没有运气修复它。

你的问题是在该行:

override func didMove(to view: SKView) { 
     gameTableView.frame = CGRect(x:14,y:100, width: frame.maxX/1.08, height: frame.maxY) 
     ... 

这事,因为你的gameTableView高度比现场高度更大:

enter image description here

为了解决您可以尝试降低高度你的表格举例:

gameTableView.frame = CGRect(x:14,y:100, width: frame.maxX/1.08, height: frame.maxY/2) 
+0

有没有anoth呃方式来做到这一点,因为我想在桌面视图的末尾有一个小空间?因为我也希望最后一个单元格在tableview滚动到底部之前的最后看起来像它的屏幕除非有一种方法来添加一个uilabel到skspritenode我需要得到这个效果,但也有小空间在tableview结尾 – Astrum

+0

这是否有任何意义,我可以发布我想要实现的图片,如果你想要的? – Astrum