TableView rows won't carry data into new section
I'm a retired high school teacher putting together a chemistry app that will include my online videos. I've gotten as far as creating a table with sections that link to a web view page...pretty successful except for one thing: the first cell row that occurs in sections after the first section (called Compounds and Reactions) will not link to their proper video, but will bring up the first video in the first section again. And the second row in the new section will bring up the second video in the first section, etc. (For example: the Periodic Table video comes up no problem when selected under the section Compounds and Reactions, but it also comes up when I select Hydrates under the Stoichiometry section) I'm convinced I don't have the right code for this part (cell!.textLabel?.text = sectionData[indexPath.section]![indexPath.row]) from the code below, but I'm not sure. Any help would be greatly appreciated to this novice. Cheers!
import UIKit
class CompoundsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let sections: [String] = ["Compounds and Reactions", "Stoichiometry"]
let s1Data: [String] = ["Periodic Table", "Molecules", "Ionic Compounds","Polyatomic Ions"]
let s2Data: [String] = ["Hydrates", "Acids"]
var sectionData: [Int: [String]] = [:]
@IBOutlet weak var tableView: UITableView!
var videos:[Video] = [Video]()
var selectedVideo:Video?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let model = VideoModel()
self.videos = model.getVideos()
self.tableView.dataSource = self
self.tableView.delegate = self
sectionData = [0 : s1Data, 1 : s2Data]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (sectionData[section]?.count)!
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "cell");
}
//Customize the cell to display the video title
cell!.textLabel?.text = sectionData[indexPath.section]![indexPath.row]
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedVideo = self.videos[indexPath.row]
//Took note of which video the viewer selected
//Call the segue
self.performSegue(withIdentifier: "goToDetail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let detailViewController = segue.destination as! VideoDetailViewController
detailViewController.selectedVideo = self.selectedVideo
}
}
swift uitableview
add a comment |
I'm a retired high school teacher putting together a chemistry app that will include my online videos. I've gotten as far as creating a table with sections that link to a web view page...pretty successful except for one thing: the first cell row that occurs in sections after the first section (called Compounds and Reactions) will not link to their proper video, but will bring up the first video in the first section again. And the second row in the new section will bring up the second video in the first section, etc. (For example: the Periodic Table video comes up no problem when selected under the section Compounds and Reactions, but it also comes up when I select Hydrates under the Stoichiometry section) I'm convinced I don't have the right code for this part (cell!.textLabel?.text = sectionData[indexPath.section]![indexPath.row]) from the code below, but I'm not sure. Any help would be greatly appreciated to this novice. Cheers!
import UIKit
class CompoundsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let sections: [String] = ["Compounds and Reactions", "Stoichiometry"]
let s1Data: [String] = ["Periodic Table", "Molecules", "Ionic Compounds","Polyatomic Ions"]
let s2Data: [String] = ["Hydrates", "Acids"]
var sectionData: [Int: [String]] = [:]
@IBOutlet weak var tableView: UITableView!
var videos:[Video] = [Video]()
var selectedVideo:Video?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let model = VideoModel()
self.videos = model.getVideos()
self.tableView.dataSource = self
self.tableView.delegate = self
sectionData = [0 : s1Data, 1 : s2Data]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (sectionData[section]?.count)!
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "cell");
}
//Customize the cell to display the video title
cell!.textLabel?.text = sectionData[indexPath.section]![indexPath.row]
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedVideo = self.videos[indexPath.row]
//Took note of which video the viewer selected
//Call the segue
self.performSegue(withIdentifier: "goToDetail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let detailViewController = segue.destination as! VideoDetailViewController
detailViewController.selectedVideo = self.selectedVideo
}
}
swift uitableview
1
How isvideos
related to the table view data (which is actually[Int: [String]]
)? You get always the same video in each section because the section parameter is not considered. I recommend a custom struct as data source.
– vadian
Nov 20 '18 at 17:26
add a comment |
I'm a retired high school teacher putting together a chemistry app that will include my online videos. I've gotten as far as creating a table with sections that link to a web view page...pretty successful except for one thing: the first cell row that occurs in sections after the first section (called Compounds and Reactions) will not link to their proper video, but will bring up the first video in the first section again. And the second row in the new section will bring up the second video in the first section, etc. (For example: the Periodic Table video comes up no problem when selected under the section Compounds and Reactions, but it also comes up when I select Hydrates under the Stoichiometry section) I'm convinced I don't have the right code for this part (cell!.textLabel?.text = sectionData[indexPath.section]![indexPath.row]) from the code below, but I'm not sure. Any help would be greatly appreciated to this novice. Cheers!
import UIKit
class CompoundsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let sections: [String] = ["Compounds and Reactions", "Stoichiometry"]
let s1Data: [String] = ["Periodic Table", "Molecules", "Ionic Compounds","Polyatomic Ions"]
let s2Data: [String] = ["Hydrates", "Acids"]
var sectionData: [Int: [String]] = [:]
@IBOutlet weak var tableView: UITableView!
var videos:[Video] = [Video]()
var selectedVideo:Video?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let model = VideoModel()
self.videos = model.getVideos()
self.tableView.dataSource = self
self.tableView.delegate = self
sectionData = [0 : s1Data, 1 : s2Data]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (sectionData[section]?.count)!
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "cell");
}
//Customize the cell to display the video title
cell!.textLabel?.text = sectionData[indexPath.section]![indexPath.row]
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedVideo = self.videos[indexPath.row]
//Took note of which video the viewer selected
//Call the segue
self.performSegue(withIdentifier: "goToDetail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let detailViewController = segue.destination as! VideoDetailViewController
detailViewController.selectedVideo = self.selectedVideo
}
}
swift uitableview
I'm a retired high school teacher putting together a chemistry app that will include my online videos. I've gotten as far as creating a table with sections that link to a web view page...pretty successful except for one thing: the first cell row that occurs in sections after the first section (called Compounds and Reactions) will not link to their proper video, but will bring up the first video in the first section again. And the second row in the new section will bring up the second video in the first section, etc. (For example: the Periodic Table video comes up no problem when selected under the section Compounds and Reactions, but it also comes up when I select Hydrates under the Stoichiometry section) I'm convinced I don't have the right code for this part (cell!.textLabel?.text = sectionData[indexPath.section]![indexPath.row]) from the code below, but I'm not sure. Any help would be greatly appreciated to this novice. Cheers!
import UIKit
class CompoundsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
let sections: [String] = ["Compounds and Reactions", "Stoichiometry"]
let s1Data: [String] = ["Periodic Table", "Molecules", "Ionic Compounds","Polyatomic Ions"]
let s2Data: [String] = ["Hydrates", "Acids"]
var sectionData: [Int: [String]] = [:]
@IBOutlet weak var tableView: UITableView!
var videos:[Video] = [Video]()
var selectedVideo:Video?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let model = VideoModel()
self.videos = model.getVideos()
self.tableView.dataSource = self
self.tableView.delegate = self
sectionData = [0 : s1Data, 1 : s2Data]
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return (sectionData[section]?.count)!
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sections[section]
}
func numberOfSections(in tableView: UITableView) -> Int {
return sections.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
if cell == nil {
cell = UITableViewCell(style: .default, reuseIdentifier: "cell");
}
//Customize the cell to display the video title
cell!.textLabel?.text = sectionData[indexPath.section]![indexPath.row]
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedVideo = self.videos[indexPath.row]
//Took note of which video the viewer selected
//Call the segue
self.performSegue(withIdentifier: "goToDetail", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let detailViewController = segue.destination as! VideoDetailViewController
detailViewController.selectedVideo = self.selectedVideo
}
}
swift uitableview
swift uitableview
edited Nov 23 '18 at 11:07
wvteijlingen
8,64612544
8,64612544
asked Nov 20 '18 at 17:20
ChemguyChemguy
1
1
1
How isvideos
related to the table view data (which is actually[Int: [String]]
)? You get always the same video in each section because the section parameter is not considered. I recommend a custom struct as data source.
– vadian
Nov 20 '18 at 17:26
add a comment |
1
How isvideos
related to the table view data (which is actually[Int: [String]]
)? You get always the same video in each section because the section parameter is not considered. I recommend a custom struct as data source.
– vadian
Nov 20 '18 at 17:26
1
1
How is
videos
related to the table view data (which is actually [Int: [String]]
)? You get always the same video in each section because the section parameter is not considered. I recommend a custom struct as data source.– vadian
Nov 20 '18 at 17:26
How is
videos
related to the table view data (which is actually [Int: [String]]
)? You get always the same video in each section because the section parameter is not considered. I recommend a custom struct as data source.– vadian
Nov 20 '18 at 17:26
add a comment |
1 Answer
1
active
oldest
votes
You probably want to say if for example indexPath.section
is equal to 0 get data from first array and in this array when for example indexPath.row
is equal to 1 get second item in this array. To achieve this, you can create array of arrays.
var videos = [["Periodic Table", "Molecules", "Ionic Compounds","Polyatomic Ions"], ["Hydrates", "Acids"]]
So in TableView delegate method you can get array depends on section index and item in this array depends on row index:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedVideo = self.videos[indexPath.section][indexPath.row]
self.performSegue(withIdentifier: "goToDetail", sender: self)
}
Alternatively, you can create your custom data model.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398273%2ftableview-rows-wont-carry-data-into-new-section%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You probably want to say if for example indexPath.section
is equal to 0 get data from first array and in this array when for example indexPath.row
is equal to 1 get second item in this array. To achieve this, you can create array of arrays.
var videos = [["Periodic Table", "Molecules", "Ionic Compounds","Polyatomic Ions"], ["Hydrates", "Acids"]]
So in TableView delegate method you can get array depends on section index and item in this array depends on row index:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedVideo = self.videos[indexPath.section][indexPath.row]
self.performSegue(withIdentifier: "goToDetail", sender: self)
}
Alternatively, you can create your custom data model.
add a comment |
You probably want to say if for example indexPath.section
is equal to 0 get data from first array and in this array when for example indexPath.row
is equal to 1 get second item in this array. To achieve this, you can create array of arrays.
var videos = [["Periodic Table", "Molecules", "Ionic Compounds","Polyatomic Ions"], ["Hydrates", "Acids"]]
So in TableView delegate method you can get array depends on section index and item in this array depends on row index:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedVideo = self.videos[indexPath.section][indexPath.row]
self.performSegue(withIdentifier: "goToDetail", sender: self)
}
Alternatively, you can create your custom data model.
add a comment |
You probably want to say if for example indexPath.section
is equal to 0 get data from first array and in this array when for example indexPath.row
is equal to 1 get second item in this array. To achieve this, you can create array of arrays.
var videos = [["Periodic Table", "Molecules", "Ionic Compounds","Polyatomic Ions"], ["Hydrates", "Acids"]]
So in TableView delegate method you can get array depends on section index and item in this array depends on row index:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedVideo = self.videos[indexPath.section][indexPath.row]
self.performSegue(withIdentifier: "goToDetail", sender: self)
}
Alternatively, you can create your custom data model.
You probably want to say if for example indexPath.section
is equal to 0 get data from first array and in this array when for example indexPath.row
is equal to 1 get second item in this array. To achieve this, you can create array of arrays.
var videos = [["Periodic Table", "Molecules", "Ionic Compounds","Polyatomic Ions"], ["Hydrates", "Acids"]]
So in TableView delegate method you can get array depends on section index and item in this array depends on row index:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.selectedVideo = self.videos[indexPath.section][indexPath.row]
self.performSegue(withIdentifier: "goToDetail", sender: self)
}
Alternatively, you can create your custom data model.
answered Nov 20 '18 at 17:35
Robert DreslerRobert Dresler
6,6142626
6,6142626
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53398273%2ftableview-rows-wont-carry-data-into-new-section%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
How is
videos
related to the table view data (which is actually[Int: [String]]
)? You get always the same video in each section because the section parameter is not considered. I recommend a custom struct as data source.– vadian
Nov 20 '18 at 17:26