Creating a TableView Programmatically with multiple cell in swift

As I am creating a TableView programmatically with multiple cell and in each cell having UICollectionView, UITableView, UITableView… but I am not able to find the error and every time when I run the program it Shows " Command failed due to signal: Segmentation fault: 11".

Has any one created this type of UI using coding.

New in Swift so forgive for errors.

// ViewController.swift
// Json Parsing in Swift
//

import UIKit
import Alamofire
import SwiftyJSON

class ViewController: UITableViewController {

var dataArray = Array()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

Alamofire.request(.GET, “http://104.131.162.14:3033/api/ios/detail”).validate().responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value {
let json = JSON(value)
print(“JSON: (json)”)

var trafficJson = json[“traffic_partners”]
trafficJson[“type”] = “Traffic”
self.dataArray.append(trafficJson)

var newsJson = json[“news”]
newsJson[“type”] = “News”
self.dataArray.append(newsJson)

var categoryJson = json[“category”]
categoryJson[“type”] = “Category”
self.dataArray.append(categoryJson)

var topFreeApps = json[“top_free_apps”]
topFreeApps[“type”] = “TopApps”
self.dataArray.append(topFreeApps)

var topSites = json[“top_sites”]
topSites[“type”] = “TopSites”
self.dataArray.append(topSites)

var trendingVideos = json[“tranding_video”]
trendingVideos[“type”] = “TrendingVideos”
self.dataArray.append(trendingVideos)

var sports = json[“sports”]
sports[“type”] = “Sports”
self.dataArray.append(sports)

var jokes = json[“jokes”]
jokes[“type”] = “jokes”
self.dataArray.append(jokes)

print(self.dataArray[0][“detail”][0].object)
print(self.dataArray[2][“detail”].object)

self.tableView.reloadData()

}
case .Failure(let error):
print(error)
}
}

tableView.registerClass(MyCell.self, forCellReuseIdentifier: “cellId”)
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) → Int {
return dataArray.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) → UITableViewCell {
let myCell = tableView.dequeueReusableCellWithIdentifier(“cellId”, forIndexPath: indexPath) as! MyCell
myCell.nameLabel.text = dataArray[indexPath.row][“type”].string
if (dataArray[indexPath.row][“type”].string == “News”) {
myCell.newsArray = dataArray[indexPath.row][“detail”].arrayObject

}

myCell.myTableViewController = self
return myCell
}

}

class MyCell: UITableViewCell {

var newsArray :NSMutableArray=[]

var myTableViewController: ViewController?

override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)

setupViews()
}

required init?(coder aDecoder: NSCoder) {
fatalError(“init(coder:) has not been implemented”)
}

let newsTableView: UITableView = {
let newsTV = UITableView(frame:UIScreen.mainScreen().bounds, style: UITableViewStyle.Plain)
newsTV.registerClass(NewsTableViewCell.self, forCellReuseIdentifier: “NewsTableViewCell”)

return newsTV
}()

func tableView(newsTableView: UITableView, numberOfRowsInSection section: Int) → Int {
return newsArray.count
}

func tableView(newsTableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) → UITableViewCell {
let myCell = newsTableView.dequeueReusableCellWithIdentifier(“cellId”, forIndexPath: indexPath) as! NewsTableViewCell
myCell.nameLabel.text = newsArray[indexPath.row][“title”].string
myCell.myTableViewController = myTableViewController
return myCell
}

let nameLabel: UILabel = {
let label = UILabel()
label.text = “Sample Item”
label.translatesAutoresizingMaskIntoConstraints = false
label.font = UIFont.boldSystemFontOfSize(14)
return label
}()

func setupViews() {
addSubview(newsTableView)
addSubview(nameLabel)

addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(“V:|[v0]|”, options: NSLayoutFormatOptions(), metrics: nil, views: [“v0”: nameLabel]))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat(“V:|[v0]|”, options: NSLayoutFormatOptions(), metrics: nil, views: [“v0”: newsTableView]))

}

func handleAction() {

}

}

It’s really incredibly difficult to spot a problem through inspection, or even attempting to recreate the view controller in a local project.
What will help you spot the problem is creating an exception breakpoint, that will hopefully get the debugger to catch the problem and provide you some information about what might be wrong. It’s easy to add one - from the debug inspector (command-7) just click the ‘+’ button and you will have the option to add an exception breakpoint. Once you have one of those the debugger should catch the error, giving you some information about just what is wrong - for instance you can figure out whether a variable is nil when it is expected to have some value, and from there you can begin working out why.

Agree with the exception breakpoint.

I might even be tempted to remove the second table and start by just adding the first tableview. Or Just by trying to create both tableviews with dummy data, without the alamo data. The reason is that Ive seen posts about this being a related to many different things including 3rd party frameworks.

Here is my project can you check oncce - http://goo.gl/8cIHGb

These are the offending lines:

// if (dataArray[indexPath.row][“type”].string == “News”) {
// myCell.newsArray = dataArray[indexPath.row][“detail”].arrayObject
// }

Comment them out and it builds. I haven’t looked at the structure of your arrays, Im still trying to understand it but the issue is here:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let myCell = tableView.dequeueReusableCellWithIdentifier("cellId", forIndexPath: indexPath) as! MyCell myCell.nameLabel.text = dataArray[indexPath.row]["type"].string if (dataArray[indexPath.row]["type"].string == "News") { print("im in") // myCell.newsArray = dataArray[indexPath.row]["detail"].arrayObject } print(dataArray[indexPath.row]["detail"].arrayObject) myCell.myTableViewController = self return myCell }

when you assign the object to your myCell.newsArray. Ill look into it further.

As i have already done till the first Section.
But now i want to add TableView, CollectionView, and so on… inside TableViewCell for each different section. Can you plz check why this error shows “Command failed due to signal: Segmentation fault: 11”

//
// ViewController.swift
// Json Parsing in Swift

import UIKit
import Alamofire
import SwiftyJSON

class ViewController: UITableViewController {

var dataArray = Array<JSON>()
//var dataArray :NSMutableArray=[]
//😜
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    
    Alamofire.request(.GET, "http://104.131.162.14:3033/api/ios/detail").validate().responseJSON { response in
        switch response.result {
        case .Success:
            if let value = response.result.value {
                let json = JSON(value)
                //print("JSON: \(json)")
                
                var trafficJson = json["traffic_partners"]
                trafficJson["type"] = "Traffic"
                self.dataArray.append(trafficJson)
                
                var newsJson = json["news"]
                newsJson["type"] = "News"
                self.dataArray.append(newsJson)
                
                
                var categoryJson = json["category"]
                categoryJson["type"] = "Category"
                self.dataArray.append(categoryJson)
                
                
                var topFreeApps = json["top_free_apps"]
                topFreeApps["type"] = "TopApps"
                self.dataArray.append(topFreeApps)
                
                
                var topSites = json["top_sites"]
                topSites["type"] = "TopSites"
                self.dataArray.append(topSites)
                
                
                var trendingVideos = json["tranding_video"]
                trendingVideos["type"] = "TrendingVideos"
                self.dataArray.append(trendingVideos)
                
                
                var sports = json["sports"]
                sports["type"] = "Sports"
                self.dataArray.append(sports)
                
                
                var jokes = json["jokes"]
                jokes["type"] = "Jokes"
                self.dataArray.append(jokes)
                
                //print(self.dataArray[0]["detail"][0].object)
                //print(self.dataArray[2]["detail"].object)
                
                self.tableView.reloadData()
            }
        case .Failure(let error):
            print(error)
        }
    }
    tableView.registerClass(MyCell.self, forCellReuseIdentifier: "cellId")
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat{
    return 200.0;//Choose your custom row height
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
    return dataArray.count
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
    let myCell = tableView.dequeueReusableCellWithIdentifier("cellId", forIndexPath: indexPath) as! MyCell
    myCell.nameLabel.text = dataArray[indexPath.row]["type"].string
    return myCell
}

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
    print(self.dataArray[indexPath.row]["type"].string)
}

}

class MyCell: UITableViewCell {

var myTableViewController: ViewController?

override init(style: UITableViewCellStyle, reuseIdentifier: String?){
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    setupViews()
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

let nameLabel: UILabel = {
    let label = UILabel()
    label.text = "Sample Item"
    label.translatesAutoresizingMaskIntoConstraints = false
    label.font = UIFont.boldSystemFontOfSize(14)
    return label
}()

func setupViews() {
    
    backgroundColor = UIColor.grayColor()
    addSubview(nameLabel)
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-12-[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
    addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": nameLabel]))
}

}

The error was bcoz i was using mutable array it get fixed by using
var newsArray = [AnyObject] ()

Can anyone handle the cellForRowAtIndexPath as i am not able to do that.