我為表情符號創建了一組陣列,并將表情符號分成適當的分組。
第 1 步:您的陣列是表情符號陣列。所以型別不再是[Emoji]。它更像 [[Emoji]]。這意味著您可以使用雙索引訪問第三組,例如:
讓 groupThirdCount = emojis[2].count
請注意, emojis.count 將不再回傳表情符號的數量,而是回傳表情符號陣列的數量。
第 2 步:更新資料源方法 numberOfSections 和 numberOfRowsInSection 以根據請求的部分回傳表情符號的數量。
編輯代碼:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//changed from 0 to 3
if section == 3 {
return emojis.count
} else {
//changed from 0 to 3
return 3
}
}
和:
override func numberOfSections(in tableView: UITableView) -> Int {
//changed from 0 to 3
return 3
}
第 3 步:更新 cellForRowAt 方法以獲取 emoji,而不是 emojis 陣列,同樣基于 section。
編輯代碼:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "emojiCell", for: indexPath)
// Configure the cell...
//added [[]] to emojis this is throwing 2 errors
let emoji = [[emojis]][indexPath.row]
cell.textLabel?.text = "\(emoji.symbol) - \(emoji.name)"
cell.detailTextLabel?.text = emoji.description
cell.showsReorderControl = true
return cell
}
問題:有人可以檢查我對代碼的編輯并在出現問題時為我指出正確的方向嗎?
完整代碼:
import UIKit
class EmojiTableViewController: UITableViewController {
var emojis: [Emoji] = [
// People Array
Emoji(symbol: "??", name: "Grinning Face", description: "A typical smiley face.", usage: "happiness"),
Emoji(symbol: "??", name: "Confused Face", description: "A confused, puzzled face.", usage: "unsure what to think; displeasure"),
Emoji(symbol: "??", name: "Heart Eyes", description: "A smiley face with hearts for eyes.", usage: "love of something; attractive"),
Emoji(symbol: "??", name: "Police Officer", description: "A police officer wearing a blue cap with a gold badge. He is smiling, and eager to help.", usage: "person of authority"),
// Animals
Emoji(symbol: "??", name: "Turtle", description: "A cute turtle.", usage: "Something slow"),
Emoji(symbol: "??", name: "Elephant", description: "A gray elephant.", usage: "good memory"),
// Other
Emoji(symbol: "??", name: "Spaghetti", description: "A plate of spaghetti.", usage: "spaghetti"),
Emoji(symbol: "??", name: "Die", description: "A single die.", usage: "taking a risk, chance; game"),
Emoji(symbol: "??", name: "Tent", description: "A small tent.", usage: "camping"),
Emoji(symbol: "??", name: "Stack of Books", description: "Three colored books stacked on each other.", usage: "homework, studying"),
Emoji(symbol: "??", name: "Broken Heart", description: "A red, broken heart.", usage: "extreme sadness"),
Emoji(symbol: "??", name: "Snore", description: "Three blue \'z\'s.", usage: "tired, sleepiness"),
Emoji(symbol: "??", name: "Checkered Flag", description: "A black and white checkered flag.", usage: "completion")
]
override func viewDidLoad() {
super.viewDidLoad()
// Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem
}
@IBAction func refreshData(_ sender: UIRefreshControl) {
tableView.reloadData()
sender.endRefreshing()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
//changed from 0 to 3
return 3
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//changed from 0 to 3
if section == 3 {
return emojis.count
} else {
//changed from 0 to 3
return 3
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "emojiCell", for: indexPath)
// Configure the cell...
let emoji = [[emojis]][indexPath.row]
//added [[]] to emojis
cell.textLabel?.text = "\(emoji.symbol) - \(emoji.name)"
cell.detailTextLabel?.text = emoji.description
cell.showsReorderControl = true
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You clicked at \(indexPath)")
}
override func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
print("You clicked emoji \(emojis[indexPath.row])")
}
/*
// Override to support conditional editing of the table view.
override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the specified item to be editable.
return true
}
*/
// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
// Delete the row from the data source
emojis.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
}
// Override to support rearranging the table view.
override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
let movingEmoji = emojis.remove(at: fromIndexPath.row)
emojis.insert(movingEmoji, at: to.row)
}
/*
// Override to support conditional rearranging of the table view.
override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
// Return false if you do not want the item to be re-orderable.
return true
}
*/
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
if let vc = segue.destination as? EmojiDetailViewController {
if let indexPath = tableView.indexPathForSelectedRow {
vc.emoji = emojis[indexPath.row]
}
}
}
}
uj5u.com熱心網友回復:
var emojis: [[Emoji]] = [
[Emoji(symbol: "??", name: "Grinning Face", description: "A typical smiley face.", usage: "happiness"),
Emoji(symbol: "??", name: "Confused Face", description: "A confused, puzzled face.", usage: "unsure what to think; displeasure"),
Emoji(symbol: "??", name: "Heart Eyes", description: "A smiley face with hearts for eyes.", usage: "love of something; attractive"),
Emoji(symbol: "??", name: "Police Officer", description: "A police officer wearing a blue cap with a gold badge. He is smiling, and eager to help.", usage: "person of authority")],
[Emoji(symbol: "??", name: "Turtle", description: "A cute turtle.", usage: "Something slow"),
Emoji(symbol: "??", name: "Elephant", description: "A gray elephant.", usage: "good memory")],
[Emoji(symbol: "??", name: "Spaghetti", description: "A plate of spaghetti.", usage: "spaghetti"),
Emoji(symbol: "??", name: "Die", description: "A single die.", usage: "taking a risk, chance; game"),
Emoji(symbol: "??", name: "Tent", description: "A small tent.", usage: "camping"),
Emoji(symbol: "??", name: "Stack of Books", description: "Three colored books stacked on each other.", usage: "homework, studying"),
Emoji(symbol: "??", name: "Broken Heart", description: "A red, broken heart.", usage: "extreme sadness"),
Emoji(symbol: "??", name: "Snore", description: "Three blue \'z\'s.", usage: "tired, sleepiness"),
Emoji(symbol: "??", name: "Checkered Flag", description: "A black and white checkered flag.", usage: "completion")]
]
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return emojis[section].count
}
override func numberOfSections(in tableView: UITableView) -> Int {
return emojis.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "emojiCell", for: indexPath)
let emoji = emojis[indexPath.section][indexPath.row]
cell.textLabel?.text = "\(emoji.symbol) - \(emoji.name)"
cell.detailTextLabel?.text = emoji.description
return cell
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/452924.html
上一篇:陣列陣列無法正確使用表情符號
