根據練習,在標題、作者或評分長于主列/s 中的標題后,表格變得不對齊(更正錯誤后:“重復計數應該是非負數”,通過將 - 更改為 以實作恒定填充需要)。我想保持列寬與給定列中最長的字串一樣長,并調整其他單元格中的間距。我試圖在有條件的 printTable 函式中解決它,但是沒有設法比較 columnLabel 和 item,因為它們在不同的回圈中。還試圖在協議中設定新功能來管理對齊。想不通,如果您可以建議。
protocol TabularDataSource {
var numberOfRows: Int { get }
var numberOfColumns: Int { get }
func label(forColumn column: Int) -> String
func itemFor(row: Int, column: Int) -> String
}
func printTable(_ dataSource: TabularDataSource & CustomStringConvertible) {
print("Tab. 1: \(dataSource)")
var headerRow = "|"
var columnWidths = [Int]()
for ii in 0 ..< dataSource.numberOfColumns {
let columnLabel = dataSource.label(forColumn: ii)
let columnHeader = " \(columnLabel) |"
headerRow = columnHeader
columnWidths.append(columnLabel.count)
}
print(headerRow)
for ii in 0 ..< dataSource.numberOfRows {
var out = "|"
for jj in 0 ..< dataSource.numberOfColumns {
let item = dataSource.itemFor(row: ii, column: jj)
let paddingNeeded = columnWidths[jj] - item.count
let padding = repeatElement(" ", count: paddingNeeded).joined(separator: "")
out = " \(padding)\(item) |"
}
print(out)
}
}
struct Book {
let title: String
let author: String
let rating: Int
}
struct BookCollection: TabularDataSource, CustomStringConvertible {
let name: String
var books = [Book]()
var description: String {
return ("\(name) book collection")
}
init(name: String) {
self.name = name
}
mutating func add(_ book: Book) {
books.append(book)
}
var numberOfRows: Int {
return books.count
}
var numberOfColumns: Int {
return 3
}
func label(forColumn column: Int) -> String {
switch column {
case 0: return "Title"
case 1: return "Author"
case 2: return "Rating"
default: fatalError("Invalid column!")
}
}
func itemFor(row: Int, column: Int) -> String {
let book = books[row]
switch column {
case 0: return book.title
case 1: return String(book.author)
case 2: return String(book.rating)
default: fatalError("Invalid column!")
}
}
}
var bookCollection = BookCollection(name: "Fantasy")
bookCollection.add(Book(title: "Ava", author: "Reno", rating: 7))
bookCollection.add(Book(title: "Vis", author: "Luc", rating: 7))
bookCollection.add(Book(title: "Te", author: "Julo", rating: 9))
printTable(bookCollection)
uj5u.com熱心網友回復:
您可以向資料源添加一個方法來了解該列的最大長度:
protocol TabularDataSource {
func width(for column: Int) -> Int
}
執行:
func width(for column: Int) -> Int {
var labels = (0..<numberOfRows).map { itemFor(row: $0, column: column )}
labels.append(label(forColumn: column))
let max = labels.max(by: { $0.count < $1.count })?.count ?? 0
return max 2 //space before/after
}
然后:
func printTable(_ dataSource: TabularDataSource & CustomStringConvertible) {
var lines = "|"
for aColumn in 0..<dataSource.numberOfColumns {
let label = dataSource.label(forColumn: aColumn)
let totalOfSpaces = dataSource.width(for: aColumn) - label.count
let spaces = repeatElement(" ", count: totalOfSpaces / 2).joined(separator: "")
let additionalSpace = totalOfSpaces % 2 == 0 ? "" : " "
lines = "\(additionalSpace)\(spaces)\(label)\(spaces)|"
}
lines = "\n"
for aRow in 0..<dataSource.numberOfRows {
lines = "|"
for aColumn in 0..<dataSource.numberOfColumns {
let label = dataSource.itemFor(row: aRow, column: aColumn)
let totalOfSpaces = dataSource.width(for: aColumn) - label.count
let additionalSpace = totalOfSpaces % 2 == 0 ? "" : " "
let spaces = repeatElement(" ", count: totalOfSpaces / 2).joined(separator: "")
lines = "\(additionalSpace)\(spaces)\(label)\(spaces)|"
}
lines = "\n"
}
print(lines)
}
這可以分解為:
func printTable(_ dataSource: TabularDataSource & CustomStringConvertible) {
func text(text: String, for width: Int) -> String {
let totalOfSpaces = width - text.count
let spaces = repeatElement(" ", count: totalOfSpaces / 2).joined(separator: "")
let additionalSpace = totalOfSpaces % 2 == 0 ? "" : " "
return "\(additionalSpace)\(spaces)\(text)\(spaces)|"
}
lines = "|"
for aColumn in 0..<dataSource.numberOfColumns {
lines = text(text: dataSource.label(forColumn: aColumn), for: dataSource.width(for: aColumn))
}
var lines = "\n"
for aRow in 0..<dataSource.numberOfRows {
lines = "|"
for aColumn in 0..<dataSource.numberOfColumns {
lines = text(text: dataSource.itemFor(row: aRow, column: aColumn), for: dataSource.width(for: aColumn))
}
lines = "\n"
}
print(lines)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/408663.html
標籤:
上一篇:iPhone上的故事板加載錯誤
