字符串插值或连接似乎不起作用 (swift)
String interpolation or concatenation does not seem to work (swift)
我在 table 查看数据源函数中有这么一小段代码 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
:
cell.textLabel?.text = "\(playerSorted[indexRow.path].username): \(playerSorted[indexRow.path].kills)"
其中.username 是一个字符串,.kills 是一个int。出于某种原因,只有用户名设法显示在标签中。我试过将 kills 转换为字符串,我试过使用连接:
cell.textLabel?.text = playerSorted[indexRow.path].username + ": " + String(playerSorted[indexRow.path].kills
但在 .username 之后没有显示任何内容,甚至连冒号也没有。我试过在 .username 之前添加一些东西,并且由于某种原因设法显示出来,如下所示:
cell.textLabel?.text = "Player \(playerSorted[indexRow.path].username): \(playerSorted[indexRow.path].kills)"
或
cell.textLabel?.text = "Player " + playerSorted[indexRow.path].username + ": " + String(playerSorted[indexRow.path].kills
在这些情况下,“玩家”和 .username 会显示在屏幕上的标签中。任何有助于解决此问题的建议都会很棒,我已经坚持了一段时间。谢谢你的时间。
完整的 collectionViewCell class:
import UIKit
class CollectionViewCellRankings: UICollectionViewCell {
@IBOutlet weak var tableViewInGame: UITableView!
var teamsSorted = [Team]()
var playersInYourTeam = [Player]()
var playersSorted = [Player]()
var cellNumber = 0
override func awakeFromNib() {
super.awakeFromNib()
}
}
extension CollectionViewCellRankings: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch cellNumber {
case 0:
return playersSorted.count
case 1:
return teamsSorted.count
case 2:
print("creating second cell")
return playersInYourTeam.count
default:
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RankingCellTV")!
cell.backgroundColor = UIColor.systemGray2
switch cellNumber {
case 0://FFA
print("Kills:")
print(playersSorted[indexPath.row].kills)
//Line that is causing the problems
cell.textLabel?.text = "\(playersSorted[indexPath.row].username): \(playersSorted[indexPath.row].kills)"
case 1://Team rankings
if Game.teamSetting > 0 {
//This line of interpolation actually works! (no idea why)
cell.textLabel?.text = "Team \(String(teamsSorted[indexPath.row].team!)): \(String(teamsSorted[indexPath.row].score!))"
switch teamsSorted[indexPath.row].team {
case 1:
cell.backgroundColor = UIColor.red
case 2:
cell.backgroundColor = UIColor.blue
case 3:
cell.backgroundColor = UIColor.green
case 4:
cell.backgroundColor = UIColor.purple
case 5:
cell.backgroundColor = UIColor.orange
case 6:
cell.backgroundColor = UIColor.cyan
case 7:
cell.backgroundColor = UIColor.yellow
case 8:
cell.backgroundColor = UIColor.magenta
default:
cell.backgroundColor = UIColor.systemBlue
}
}
case 2://Players in team
print("printing team player")
cell.textLabel?.text = "\(playersInYourTeam[indexPath.row].username) | K: \(playersInYourTeam[indexPath.row].kills) D: \(playersInYourTeam[indexPath.row].deaths)"
default:
break
}
return cell
}
}
UIViewController class 集合视图委托和数据源方法:
extension InGameVC: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if Game.teamSetting > 0 {
return 2
} else {
return 1
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = inGameCV.dequeueReusableCell(withReuseIdentifier: "RankingCell", for: indexPath) as! CollectionViewCellRankings
cell.backgroundColor = UIColor.systemGray2
if indexPath.row == 0 {
cell.tableViewInGame.backgroundColor = UIColor.systemGray2
if Game.teamSetting > 0 {
print(indexPath.row)
cell.cellNumber = indexPath.row + 1
cell.playersInYourTeam = playersInYourTeam
cell.teamsSorted = teamsSorted
} else {
cell.cellNumber = 0
cell.playersSorted = playersSorted
}
}
cell.tableViewInGame.delegate = cell
cell.tableViewInGame.dataSource = cell
// cell.tableViewInGame.separatorStyle = UITableViewCell.SeparatorStyle.none
cell.tableViewInGame.reloadData()
return cell
}
}
Player 和 Team 都是结构体。
因此,请准确检查 playerSorted[indexRow.path].username。它可能以换行结束。然后你看不到杀戮(顺便说一句,也没有分号)。
我在 table 查看数据源函数中有这么一小段代码 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
:
cell.textLabel?.text = "\(playerSorted[indexRow.path].username): \(playerSorted[indexRow.path].kills)"
其中.username 是一个字符串,.kills 是一个int。出于某种原因,只有用户名设法显示在标签中。我试过将 kills 转换为字符串,我试过使用连接:
cell.textLabel?.text = playerSorted[indexRow.path].username + ": " + String(playerSorted[indexRow.path].kills
但在 .username 之后没有显示任何内容,甚至连冒号也没有。我试过在 .username 之前添加一些东西,并且由于某种原因设法显示出来,如下所示:
cell.textLabel?.text = "Player \(playerSorted[indexRow.path].username): \(playerSorted[indexRow.path].kills)"
或
cell.textLabel?.text = "Player " + playerSorted[indexRow.path].username + ": " + String(playerSorted[indexRow.path].kills
在这些情况下,“玩家”和 .username 会显示在屏幕上的标签中。任何有助于解决此问题的建议都会很棒,我已经坚持了一段时间。谢谢你的时间。
完整的 collectionViewCell class:
import UIKit
class CollectionViewCellRankings: UICollectionViewCell {
@IBOutlet weak var tableViewInGame: UITableView!
var teamsSorted = [Team]()
var playersInYourTeam = [Player]()
var playersSorted = [Player]()
var cellNumber = 0
override func awakeFromNib() {
super.awakeFromNib()
}
}
extension CollectionViewCellRankings: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
switch cellNumber {
case 0:
return playersSorted.count
case 1:
return teamsSorted.count
case 2:
print("creating second cell")
return playersInYourTeam.count
default:
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RankingCellTV")!
cell.backgroundColor = UIColor.systemGray2
switch cellNumber {
case 0://FFA
print("Kills:")
print(playersSorted[indexPath.row].kills)
//Line that is causing the problems
cell.textLabel?.text = "\(playersSorted[indexPath.row].username): \(playersSorted[indexPath.row].kills)"
case 1://Team rankings
if Game.teamSetting > 0 {
//This line of interpolation actually works! (no idea why)
cell.textLabel?.text = "Team \(String(teamsSorted[indexPath.row].team!)): \(String(teamsSorted[indexPath.row].score!))"
switch teamsSorted[indexPath.row].team {
case 1:
cell.backgroundColor = UIColor.red
case 2:
cell.backgroundColor = UIColor.blue
case 3:
cell.backgroundColor = UIColor.green
case 4:
cell.backgroundColor = UIColor.purple
case 5:
cell.backgroundColor = UIColor.orange
case 6:
cell.backgroundColor = UIColor.cyan
case 7:
cell.backgroundColor = UIColor.yellow
case 8:
cell.backgroundColor = UIColor.magenta
default:
cell.backgroundColor = UIColor.systemBlue
}
}
case 2://Players in team
print("printing team player")
cell.textLabel?.text = "\(playersInYourTeam[indexPath.row].username) | K: \(playersInYourTeam[indexPath.row].kills) D: \(playersInYourTeam[indexPath.row].deaths)"
default:
break
}
return cell
}
}
UIViewController class 集合视图委托和数据源方法:
extension InGameVC: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if Game.teamSetting > 0 {
return 2
} else {
return 1
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = inGameCV.dequeueReusableCell(withReuseIdentifier: "RankingCell", for: indexPath) as! CollectionViewCellRankings
cell.backgroundColor = UIColor.systemGray2
if indexPath.row == 0 {
cell.tableViewInGame.backgroundColor = UIColor.systemGray2
if Game.teamSetting > 0 {
print(indexPath.row)
cell.cellNumber = indexPath.row + 1
cell.playersInYourTeam = playersInYourTeam
cell.teamsSorted = teamsSorted
} else {
cell.cellNumber = 0
cell.playersSorted = playersSorted
}
}
cell.tableViewInGame.delegate = cell
cell.tableViewInGame.dataSource = cell
// cell.tableViewInGame.separatorStyle = UITableViewCell.SeparatorStyle.none
cell.tableViewInGame.reloadData()
return cell
}
}
Player 和 Team 都是结构体。
因此,请准确检查 playerSorted[indexRow.path].username。它可能以换行结束。然后你看不到杀戮(顺便说一句,也没有分号)。