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<JSON>() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. Alamofire.request(.GET, "").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() { } }


class MyViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

let tableView = UITableView()

override func viewDidLoad() {

// Set the table view's frame
tableView.frame = CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height)

// Set the data source and delegate
tableView.dataSource = self
tableView.delegate = self

// Register the custom cell class
tableView.register(CustomTableViewCell.self, forCellReuseIdentifier: "CustomCell")

// Add the table view as a subview

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CustomCell", for: indexPath) as! CustomTableViewCell
cell.textLabel?.text = "Row \(indexPath.row)"
return cell

Answered 2 months ago

Unlock Startups Unlimited

Access 20,000+ Startup Experts, 650+ masterclass videos, 1,000+ in-depth guides, and all the software tools you need to launch and grow quickly.

Already a member? Sign in

Copyright © 2023 LLC. All rights reserved.