は、私は複数のセクションで私のテーブルビュースミス複数行の設定方法は次のとおりです。この構造体を作成し、その下に、作成、のViewControllerクラスの先頭に
1)を変数
struct Objects {
var sectionName : String!
var sectionObjects: [String]!
}
var objectsArray = [Objects]()
2)のviewDidLoadでは、numberOfSectionsInTableView
について)可変
objectsArray = [Objects(sectionName: "Find People", sectionObjects: ["Facebook", "Twitter", "Contacts"]), Objects(sectionName: "Your Account", sectionObjects: ["Edit Info", "Delete Account"]), Objects(sectionName: "Support", sectionObjects: ["Help", "Report a Problem", "Contact Us"]), Objects(sectionName: "App Info", sectionObjects: ["About", "Open Source Libraries", "Privacy Policy", "Our Twitter", "Our Facebook"])]
セット3発生する任意のアクションのためにtitleForHeaderInSection
override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return objectsArray[section].sectionName
}
7)を設定するためのcellForRowAtIndexPath
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = objectsArray[indexPath.section].sectionObjects[indexPath.row]
return cell
}
6)についてnumberOfRowsInSection
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return objectsArray[section].sectionObjects.count
}
5)については
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return objectsArray.count
}
4)ユーザーがセルをタップすると、didSelectRowAtIndexPaを呼び出す必要がありますth - >私の場合、4つのセクション(ヘッダー)があるので、4つのスイッチを作成する必要があります(ケースは0ではなく1から始まります)。次に、各セクションについて、各セクションの各行に対して大文字小文字を設定する必要があります。その場合、必要なものを呼び出すことができます。たとえば、別のviewControllerに対してsegueを実行することができます。デフォルトでは、基本的にif else文のelseを参照しています。他のすべてが失敗した場合、何かが正しく動作しなかったというエラーをユーザに表示します。ビルトインされたalertViewを使うことができますが、私はGitHubでSCLAlertViewを使うのが好きです!あなたのケースでは
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if reachabilityStatus == kNOTREACHABLE {
self.displayError("No Internet Connection", message: "Please connect to the internet before continuing")
} else {
switch(indexPath.section) {
case 0:
switch(indexPath.row) {
case 0:
// Find People through facebook -> test below
print("Connect with FB")
return
case 1:
// Find People through twitter -> test below
print("Connect with Twitter")
return
case 2:
// Find People through contacts -> test below
print("Connect with Contacts")
return
default:
self.displayError("Something Went Wrong", message: "We're sorry, it seems like we can't open that for you. Please try again.")
return
}
case 1:
switch(indexPath.row) {
case 0:
// Edit Account -> test below
self.performSegueWithIdentifier("editProfileSegue", sender: self)
return
case 1:
// Delete Account -> test below
print("Delete Account")
return
default:
self.displayError("Something Went Wrong", message: "We're sorry, it seems like we can't open that for you. Please try again.")
return
}
case 2:
switch(indexPath.row) {
case 0:
// Help -> test below
print("Help")
return
case 1:
// Report a Problem -> test below
print("Report Problem")
return
case 2:
// Contact Us -> test below
print("Contact Us")
return
default:
self.displayError("Something Went Wrong", message: "We're sorry, it seems like we can't open that for you. Please try again.")
return
}
case 3:
switch(indexPath.row) {
case 0:
// About App -> test below
print("About App")
return
case 1:
// Open Source Libraries -> test below
print("Open Source Libraries")
return
case 2:
// Privacy Policy -> test below
print("Privacy Policy")
return
case 3:
// Our Twitter -> Test Below
print("Our Twitter")
case 4:
// Our Facebook -> Test Below
print("Our Facebook")
return
default:
self.displayError("Something Went Wrong", message: "We're sorry, it seems like we can't open that for you. Please try again.")
return
}
default:
self.displayError("Something Went Wrong", message: "We're sorry, it seems like we can't open that for you. Please try again.")
print("last error")
return
} // close section switch
}
}
、あなただけの3つのセクションを持っているので、ちょうど
が何か助けが必要な場合は、私に教えてくださいどのような作品を参照するためのコードで遊んで!
'cellForRowAtIndexPath'のコードを追加できますか? – kye
cellForRowAtIndexPathメソッド内には何がありますか? – bolnad
'numberOfRowsInSection'から定数を返すことはまれです。通常、ソースデータ配列からのカウントを返します。 – Paulw11