I can't display TableView in added subview - ios

I have a ViewController named LiveMatchVC. I want to show another ViewController(named LiveMatchMomentsVC and it has a TableView) in LiveMatchVC.
I added a subview and I displayed LiveMatchMomentsVC in LiveMatchVC, but I can't view the TableView of the LiveMatchMoments in LiveMatchVC.
My codes and screenshots are as below.
How can I solve this problem?
White area is my TableView in LiveMatchMomentsVC
LiveMatchVC Codes
class LiveMatchVC: UIViewController {
//---------Outlets-----------
#IBOutlet weak var imgLiveMatchScoreBG: UIImageView!
#IBOutlet weak var openLeft: UIBarButtonItem!
let titleCont1 = TZSegmentedControl(sectionTitles: ["Maç Raporu","Canlı Anlatım", "Önemli Olaylar", "İstatistikler", "Kadrolar" ])
override func viewDidLoad() {
super.viewDidLoad()
let liveMatchMomentsVC = storyboard?.instantiateViewController(withIdentifier: "LiveMatchMomentsVC") as! LiveMatchMomentsVC
titleCont1.indexChangeBlock = { (index) in
if self.titleCont1.selectedSegmentIndex == 1 {
self.view.addSubview(liveMatchMomentsVC.view)
liveMatchMomentsVC.view.frame = CGRect(x: 0, y: self.imgLiveMatchScoreBG.frame.height+self.titleCont1.frame.height, width: self.view.frame.width, height: self.view.frame.height-44)
self.addChildViewController(liveMatchMomentsVC)
self.view.addSubview(liveMatchMomentsVC.view)
self.didMove(toParentViewController: self)
}
}
}
}
LiveMatchMomentsVC Codes
import UIKit
class LiveMatchMomentsVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tblLiveMatch: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 15
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MatchEvent", for: indexPath) as! MatchEventTableViewCell
cell.lblMatchEventTime.text = "69"
cell.lblMatchEvent.text = "Goal. Juraj Kucka"
return cell
}
}

Related

Table view cell not showing in table view controller

I'm new to swift and I'm trying to get my cells to show in tableView. I searched for questions here but none could help. The table loads fine and I get no errors regarding the tableView. Is there something I'm missing?
-I've double-checked the reuse identifier, they are the same
-I removed and readied the outlets
Here is my UITableViewController class:
class DrawerViewController: UITableViewController{
#IBOutlet weak var drawerTable: UITableView!
var currentCell = [DrawerModel]()
override func viewDidLoad() {
super.viewDidLoad()
addGradientToView(view: drawerTable)
let temp2Model = DrawerModel()
temp2Model.item = "Profile"
temp2Model.icon = "loginbackground2.jpg"
currentCell.append(temp2Model)
drawerTable.delegate = self
drawerTable.dataSource = self
//self.drawerTable.separatorStyle = UITableViewCellSeparatorStyle.none
//self.drawerTable.reloadData()
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = drawerTable.dequeueReusableCell(withIdentifier: "drawerCell", for: indexPath) as! DrawerTableViewCell
let tempModel: DrawerModel
tempModel = currentCell[indexPath.row]
cell.thisLabel.text = tempModel.item
print("test1" + tempModel.item! as Any)
let currentImage = UIImage(named: "loginbackground2.jpg")
cell.thisImage = UIImageView(image: currentImage)
return cell
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1;
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("it ran")
return currentCell.count
}
func addGradientToView(view: UITableView)
{
//gradient layer
let gradientLayer = CAGradientLayer()
//define colors
gradientLayer.colors = [UIColor.init(rgb: 0x4b0082).cgColor, UIColor.init(rgb: 0xfa8072).cgColor]
//define locations of colors as NSNumbers in range from 0.0 to 1.0
//if locations not provided the colors will spread evenly
gradientLayer.locations = [0.0, 0.8]
//define frame
gradientLayer.frame = view.bounds
//insert the gradient layer to the view layer
view.layer.insertSublayer(gradientLayer, at: 0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Here is my UITableViewCell class:
class DrawerTableViewCell: UITableViewCell {
#IBOutlet weak var thisImage: UIImageView!
#IBOutlet weak var thisLabel: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
If you are using UITableViewController (in your case DrawerViewController) it already have a tableView property embedded, which have a delegate and dataSource set to controller.
So when in your case next lines are redundant
drawerTable.delegate = self
drawerTable.dataSource = self
To fix just call tableView.reload() after currentCell.append(temp2Model)

How to create new cells when button on cell is pressed in tableview

*I want to insert new rows each time when add button is pressed ( condition: a valid email address is entered).
enter image description here
Here is my code:
import UIKit
var totalCells=0
class AddEmailTableCell: UITableViewCell,UITextFieldDelegate {
var obj:UIViewController=ViewController()
#IBOutlet weak var textField: UITextField!
#IBOutlet weak var addButton: UIButton!
#IBOutlet weak var addEmailLabel: UILabel!
#IBOutlet weak var addEmailImg: UIImageView!
func isValidEmail(testStr:String) -> Bool {
// print("validate calendar: \(testStr)")
let emailRegEx = "[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
let emailTest = NSPredicate(format:"SELF MATCHES %#", emailRegEx)
return emailTest.evaluate(with: testStr)
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
addButton.isHidden=true
self.textField.delegate=self
}
#IBAction func addEmailButton(_ sender: Any) {
if(textField.text == nil || (textField.text?.isEmpty)! ) {
print("email not filled")
return
}
if (isValidEmail(testStr: textField.text!)==false){
print("email not valid")
return
}
totalCells=totalCells+1
let index=IndexPath(row: totalCells, section: 0)
(obj as! ViewController).tableView.insertRows(at: [index], with: UITableViewRowAnimation.automatic)
print(totalCells)
print("valid")
}
}
My viewController code is given here:
import UIKit
class ViewController:
UIViewController,UITableViewDelegate,UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if totalCells==0{
return 1
}
return totalCells
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell=tableView.dequeueReusableCell(withIdentifier: "addEmail", for: indexPath) as! AddEmailTableCell
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 60
}
}
so what should i do here, I am calling tableview.reloadData and passing updated number of cells to my tableview, but it is not working.
You should move isValidEmail() and addEmailButton() to your ViewController and control tableview in this controller.

How to perform a segue view from custom UITableViewCell(xib) to another ViewController

I want to show a button on my custom UITableViewCell which takes the user to another screen on tapping on it.
I have tried following code but it doesn't work
Child view:
#IBAction func childScreenButton(sender: AnyObject) {
if let delegate = self.delegate {
delegate.childButtonClickedOnCell(self)
}
}
Protocol:
protocol childTableCellDelegate: class {
func childButtonClickedOnCell(cell: childViewCell)
}
Parent ViewController:
func childButtonClickedOnCell(cell: FeedChildViewCell) {
self.clickedIndexPath = self.feedsTableView.indexPathForCell(cell)
self.performSegueWithIdentifier("toNextScreen", sender: self)
}
while I'm testing the break point doesn't enter into "delegate.childButtonClickedOnCell(self)" on child view. Please let me know if am doing anything wrong here. Thanks!!
I suspect you've got a couple things out of place, or not defined just right.
I just ran a quick test with this, and the delegate call works fine... see if you notice anything not-quite-the-same...
//
// TestTableViewController.swift
//
// Created by DonMag on 4/7/17.
// Copyright © 2017 DonMag. All rights reserved.
//
import UIKit
protocol MyCellDelegate {
func pressedButtonForMyCell(theSender: MyCell)
}
class MyCell: UITableViewCell {
#IBOutlet weak var theLabel: UILabel!
#IBOutlet weak var theButton: UIButton!
var delegate: MyCellDelegate?
#IBAction func childScreenButton(sender: AnyObject) {
delegate?.pressedButtonForMyCell(theSender: self)
}
}
class TestTableViewController: UITableViewController, MyCellDelegate {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath) as! MyCell
cell.theLabel.text = "\(indexPath)"
cell.delegate = self
return cell
}
func pressedButtonForMyCell(theSender: MyCell) {
print("delegate called", theSender)
}
}

Gets number of rows but doesn't print

I have a program written in Swift 3, that grabs JSON from a REST api and appends it to a table view.
Right now, I'm having troubles with getting it to print in my Tableview, but it does however understand my count function.
So, I guess my data is here, but it just doesn't return them correctly:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, HomeModelProtocal {
#IBOutlet weak var listTableView: UITableView!
func itemsDownloaded(items: NSArray) {
feedItems = items
self.listTableView.reloadData()
}
var feedItems: NSArray = NSArray()
var selectedLocation : Parsexml = Parsexml()
override func viewDidLoad() {
super.viewDidLoad()
self.listTableView.delegate = self
self.listTableView.dataSource = self
let homeModel = HomeModel()
homeModel.delegate = self
homeModel.downloadItems()
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier: String = "BasicCell"
let myCell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)!
let item: Parsexml = feedItems[indexPath.row] as! Parsexml
myCell.textLabel!.text = item.title
return myCell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return feedItems.count
}
override func viewDidAppear(_ animated: Bool) {
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Are you by any chance able to see the error that I can't see?
Note. I have not added any textlabel to the tablerow, but I guess that there shouldn't be added one, when its custom?
Try this code:
override func viewDidLoad() {
super.viewDidLoad()
print(yourArrayName.count) // in your case it should be like this print(feedItems.count)
}

second TableView Cell dissapear in my TabBarController

I got an TabBar bassed app (using Swift) and i got 2 table views in different ViewControllers, and just the table that is in the first View, but isnt in other views.
I check the display of the cells and is ok.
If i put another view (without a table as first view) the other views within table views dont appear also.
some images of my problem
http://s12.postimg.org/7c58vxfe5/Captura_de_pantalla_2015_10_03_a_las_17_25_01.png
http://s13.postimg.org/meysu5j0n/Captura_de_pantalla_2015_10_03_a_las_17_24_48.png
Thanks in advance !
Edit:
Some code
import UIKit
class HomeViewController: UIViewController , UITableViewDataSource , UITableViewDelegate , UITabBarControllerDelegate {
let CellIdentifier = "CellTarea";
#IBOutlet var tareasTable: UITableView!
let tareas = ["Revisar etiquetado leche Nido","Reponer yoghurt sin lactosa","Reponer Chocolates Sahne Nuss","Revisar etiquetado de Chamito","Reponer Nescafe Clasico 200 gr."]
override func viewDidLoad() {
super.viewDidLoad()
self.tareasTable.delegate=self
self.tareasTable.dataSource=self
//Tabbar Config
self.tabBarController?.delegate = self;
for item in (self.tabBarController?.tabBar.items as NSArray!){
(item as! UITabBarItem).image = (item as! UITabBarItem).image?.imageWithRenderingMode(.AlwaysOriginal)
(item as! UITabBarItem).selectedImage = (item as! UITabBarItem).selectedImage?.imageWithRenderingMode(.AlwaysOriginal)
}
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: - TableView
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell:CustomTareasCell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier, forIndexPath: indexPath) as! CustomTareasCell
cell.tareaLabel.text = tareas[indexPath.row];
cell.indiceLabel.text = String(indexPath.row+1);
return cell
}
Other view controller with tableview
import UIKit
class ReportesViewController: UIViewController, UITableViewDelegate , UITableViewDataSource, UITabBarControllerDelegate {
let personas = ["Alexis Parra","Ernesto Jímenez","Paulina Torres","Juan Soto","Julio Gallardo"]
let fechas = ["24 de Septimbre","22 de Septimbre", "21 de Septimbre", "20 de Septimbre","18 de Septimbre"]
let textCellIdentifier = "CellReporte";
#IBOutlet var reportesTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.reportesTable.delegate=self;
self.reportesTable.dataSource=self;
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(animated: Bool) {
let topOffest = CGPointMake(0, -(self.reportesTable.contentInset.top))
self.reportesTable.contentOffset = topOffest
}
//MARK: - TableView
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let reportesCell:CustomReportesCell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! CustomReportesCell
print(fechas[indexPath.row])
reportesCell.fecha.text = fechas[indexPath.row];
reportesCell.autor.text = personas[indexPath.row];
return reportesCell
}

Resources