【问题标题】:Spritekit - Tableview scrolling cut offSpritekit - Tableview 滚动被切断
【发布时间】:2017-04-09 16:56:25
【问题描述】:

我目前正在我的 sprite kit 游戏中开发一个菜单屏幕来显示所有项目,我使用 tableview 来实现这一点,因为它允许我有一个用于项目描述的 uilabel。 我的 uitableview 子类如下:

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)!")
  }

}

游戏场景:

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 的滚动更改为更长,以便我可以完全看到所有单元格?我已经尝试在这里查看其他答案,但我没有运气修复它。

【问题讨论】:

    标签: ios swift xcode uitableview sprite-kit


    【解决方案1】:

    你的问题是:

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

    这是因为您的gameTableView 高度大于场景高度:

    要解决,您可以尝试降低桌子的高度,例如:

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

    【讨论】:

    • 是否有另一种方法可以做到这一点,因为我想在 tableview 的末尾有一个小空间?因为我还希望在表格视图滚动到底部之前最后的最后一个单元格看起来像是从屏幕上消失了,除非有办法将 uilabel 添加到 skspritenode 我需要获得这种效果但也有小空间在表格视图的末尾
    • 如果你愿意,我可以张贴我正在努力实现的目标,这有什么意义吗?
    猜你喜欢
    • 2016-02-07
    • 1970-01-01
    • 2019-04-15
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    相关资源
    最近更新 更多