I've had this problem for a very long time. I'm trying to populate a UITableView with NSMutableArray. But I'm not getting all the objects out side of the loop. The total count of objects is 167. But instead it returns 167 of the same object.
I declared the array outside of the loop like this:
class TableViewController: UITableViewController {
var sermon = SermonLink()
var sermons: NSMutableArray = []
override func viewDidLoad() {
super.viewDidLoad()
loadContent()
}
The whole thing happens in a method:
func loadContent() {
var elements: NSArray = []
let urlForHTML = NSURL(string:"http://ontherock.dk/?page_id=1141")
var dataForHTML: NSData = NSData(contentsOfURL: urlForHTML!)!
var htmlParser = TFHpple(HTMLData: dataForHTML)
var htmlXPathString = "//td[@class='lyf_td_filename']/a"
elements = htmlParser.searchWithXPathQuery(htmlXPathString) as NSArray
var sermonArr:NSMutableArray = []
for item in elements {
var element: TFHppleElement = item as! TFHppleElement
var firstChildItem: NSString = element.firstChild.content
var dashSeperatedSermonArray = firstChildItem.componentsSeparatedByString("-") as! [String]
sermon.subject = dashSeperatedSermonArray[4].stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
sermon.speaker = dashSeperatedSermonArray[3].stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
sermon.urlForAudioSermon = element.attributes["href"] as! String
sermonArr.addObject(sermon)//Here inside the loop I get the expected data
}
sermons = sermonArr //sermons contains only the object at the last index, 167 times
self.tableView.reloadData()
}
SermonLinka class?sermonobject over and over. You need to create a newsermonobject inside the loop before the linessermon.subject = ...SermonLinkis a class. Okay. I'll try declaring the object inside as you suggest