Can someone tell me why it don't return cell for me? I really don't now why. On "cellTableViewCell" I have only outlets with label's.
Or I need to read at func viewWillAppear?
var nazwaTab:[String] = []
func getContext() -> NSManagedObjectContext {
let appDelegate = UIApplication.shared.delegate as? AppDelegate
return (appDelegate?.persistentContainer.viewContext)!
}
func wczytaj(wartosc:String) -> String {
var wartosc2 = wartosc
let request: NSFetchRequest<Ostatnie> = Ostatnie.fetchRequest()
do {
let result = try getContext().fetch(request)
for liczby in result {
if let str = liczby.value(forKey: "nazwa") as? String
{
wartosc2 = str
nazwaTab.append(wartosc2)
nazwaTab.last
}
print(nazwaTab)
}
} catch {
print(error)
}
return wartosc2
}
}
extension tabViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return nazwaTab.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! cellTableViewCell
cell.dlugoscLabel.text = dlugoscTab[indexPath.row] as! String
cell.nazwaLabel.text = nazwaTab[indexPath.row]
cell.szerokoscLabel.text = szerokoscTab[indexPath.row] as! String
return cell
}
}
I think you should set UITableViewDelegate like this :
class tabViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
and in viewDidLoad:
tableView.delegate = self
tableView.dataSource = self
Related
This question already has answers here:
Class 'ViewController' has no initializers in swift
(8 answers)
Closed last month.
import UIKit
import CoreData
class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var titleArray = [String]()
var idArray = [UUID()]
var chosenTitle = ""
var chosenTitleID : UUID
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.topItem?.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.add, target: self, action: #selector(addButtonClicked))
tableView.delegate = self
tableView.dataSource = self
getData()
}
func getData() {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Places")
request.returnsObjectsAsFaults = false
do {
let results = try context.fetch(request)
if results.count > 0 {
self.titleArray.removeAll(keepingCapacity: false)
self.idArray.removeAll(keepingCapacity: false)
for result in results as! [NSManagedObject] {
if let title = result.value(forKey: "title") as? String {
self.titleArray.append(title)
}
if let id = result.value(forKey: "id") as? UUID {
self.idArray.append(id)
}
tableView.reloadData()
}
}
} catch {
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = titleArray[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return titleArray.count
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
chosenTitle = titleArray[indexPath.row]
chosenTitleID = idArray[indexPath.row]
performSegue(withIdentifier: "toDetailsVC", sender: nil)
}
I have a problem with Swift class. I have a swift file for UITableViewController class. I had "Class 'TableViewController' has no initializers" problem.
I didn't find anything but the code says TableViewController has no initializers. I don't understand this problem. I have version 14.2.
Thanks for your responses.
You can make chosenTitleID property optional(var chosenTitleID: UUID?) or provide the default value to chosenTitleID because TableViewController class contains non-optional property.
My app is using Firebase Realtime Database to store information of each user. The tableView works fine. I added a searchBar and when I type letters in the searchBar, I can see that the amount of rows presented in the tableView is equal to the amount of users which contain these letters in their names. The problem is that the rows presented in the tableview (after typing letters in the search bar), contain the information of the first user till x user (x = amount of users which contain these letters in their names).
import UIKit
import FirebaseDatabase
import Firebase
class VideoListScreen: UIViewController {
#IBOutlet weak var tableView: UITableView!
var blogPost: [BlogPost] = []
var searchBlogPost = [BlogPost]()
var searching = false
override func viewDidLoad() {
super.viewDidLoad()
blogPost = []
createArray()
tableView.delegate = self
tableView.dataSource = self
}
func createArray() {
let ref = Database.database().reference().child("Users")
ref.observe(.childAdded, with: { (snapshot) in
if let postDict = snapshot.value as? [String : String] {
let post = BlogPost(name:postDict["name"] ?? "" , gaf: postDict["gaf"] ?? "", place: postDict["place"] ?? "", phone: postDict["phone"] ?? "", notes: postDict["notes"] ?? "", elsertext: postDict["elsertext"] ?? "")
self.blogPost.append(post)
self.tableView.reloadData()
}
})
}
}
extension VideoListScreen: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if searching{
return searchBlogPost.count
}else{
return blogPost.count
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let blogp = blogPost[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "Tavla") as! Tavla
if searching{
cell.setBLogPost(blogPost: searchBlogPost[indexPath.row])
}
else{
cell.setBLogPost(blogPost: blogPost[indexPath.row])
}
cell.setBLogPost(blogPost: blogp)
return cell
}
}
extension VideoListScreen: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchBlogPost = blogPost.filter({$0.name.prefix(searchText.count) == searchText})
searching = true
tableView.reloadData()
}
}
I Believe the problem is in the if and else statments in this function:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let blogp = blogPost[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "Tavla") as! Tavla
if searching{
cell.setBLogPost(blogPost: searchBlogPost[indexPath.row])
}
else{
cell.setBLogPost(blogPost: blogPost[indexPath.row])
}
cell.setBLogPost(blogPost: blogp)
return cell
}
You have extra cell.setBLogPost(blogPost: blogp) in your delegate methode
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let blogp = blogPost[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "Tavla") as! Tavla
if searching{
cell.setBLogPost(blogPost: searchBlogPost[indexPath.row])
}
else{
cell.setBLogPost(blogPost: blogPost[indexPath.row])
}
return cell
}
I think this will solve your problem and you can use
import UIKit
import FirebaseDatabase
import Firebase
class VideoListScreen: UIViewController {
#IBOutlet weak var tableView: UITableView!
var blogPost: [BlogPost] = []
var searchBlogPost = [BlogPost]()
override func viewDidLoad() {
super.viewDidLoad()
blogPost = []
createArray()
tableView.delegate = self
tableView.dataSource = self
}
func createArray() {
let ref = Database.database().reference().child("Users")
ref.observe(.childAdded, with: { (snapshot) in
if let postDict = snapshot.value as? [String : String] {
let post = BlogPost(name:postDict["name"] ?? "" , gaf: postDict["gaf"] ?? "", place: postDict["place"] ?? "", phone: postDict["phone"] ?? "", notes: postDict["notes"] ?? "", elsertext: postDict["elsertext"] ?? "")
self.blogPost.append(post)
self.searchBlogPost = self.blogPost
self.tableView.reloadData()
}
})
}
}
extension VideoListScreen: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return searchBlogPost.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let blogp = searchBlogPost[indexPath.row]
let cell = tableView.dequeueReusableCell(withIdentifier: "Tavla") as! Tavla
cell.setBLogPost(blogPost: blogp)
return cell
}
}
extension VideoListScreen: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
if ((searchText.trimmingCharacters(in: .whitespaces).isEmpty) {
searchBlogPost = blogPost
} else {
searchBlogPost = blogPost.filter({$0.name.prefix(searchText.count) == searchText})
}
tableView.reloadData()
}
}
I hope this will helps.
I have one viewController. In that i have one button, if i click on that button am presenting a UIView on that viewController.
In that UIVew i have one tableView. Now i want to pass data into that tableview, which am getting from server.
I cant display the data in tableView, i kept breakpoint and checked. am not able to enter into cellForRowAt indexPath method also
could any one help me with this
Here is the code which i tried
Here is my UIView class
class ButtonClicked: UIView {
#IBOutlet weak var tableView: UITableView!
override func didMoveToSuperview() {
//super.awakeFromNib()
}
Here is my ViewController class
class ViewController: UIViewController{
var tableviewDisplayArray: NSArray = []
override func viewDidLoad() {
super.viewDidLoad()
buttonClicked.tableView.register(UINib(nibName: “TableViewDisplayCell", bundle: nil), forCellReuseIdentifier: “tableViewDispCell")
buttonClicked.tableView.delegate = self
buttonClicked.tableView.dataSource = self
}
#IBAction func addMoneyButtonClicked() {
buttonClickedWebserviceCall()
actionAlertViewController.actionType = ActionAlertType.ADD_MONEY
present(self.view.actionAlertPopup(alertVC: actionAlertViewController), animated: animated, completion: nil)
}
func buttonClickedWebserviceCall(){
let params: NSDictionary = ["langId" : “1”, "countryId" : “1”]
callingWebservice().dataTaskWithPostRequest(urlrequest: URL_BUTTONCLICKED viewcontroller: self, params: params) { (result, status) in
let response : NSDictionary = result as! NSDictionary
let status = response.value(forKey: "httpCode") as! NSNumber
if status == 200{
DispatchQueue.main.async {
self.tableviewDisplayArray= (response.value(forKey: “response”) as? NSArray)!
print(self.tableviewDisplayArray)
self.buttonClicked.tableView.reloadData()
}
}
else{
DispatchQueue.main.async {
}
}
}
}//method close
}//class close
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == buttonClicked.tableView {
return tableviewDisplayArray.count
}
else{
return 5
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if tableView == buttonClicked.tableView {
return 30.0
}
else{
return 75.0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == buttonClicked.tableView {
let cell = buttonClicked.tableView.dequeueReusableCell(withIdentifier: "tableViewDispCell", for: indexPath) as! TableViewDisplayCell
let storedArray = self.tableviewDisplayArray.object(at: indexPath.row) as! NSDictionary
print(storedArray)
return cell
}
else{
let cell = tableView.dequeueReusableCell(withIdentifier: “normalCell”, for: indexPath) as! NormalCell
return cell
}
}
}
You have written the tableView delegate methods in an extension of UIViewController class. Just write that code inside the ViewController class where you have set the delegate and datasource to.Like this
class ViewController: UIViewController,UITableViewDelegate, UITableViewDataSource{
var tableviewDisplayArray: NSArray = []
override func viewDidLoad() {
super.viewDidLoad()
buttonClicked.tableView.register(UINib(nibName: “TableViewDisplayCell", bundle: nil), forCellReuseIdentifier: “tableViewDispCell")
buttonClicked.tableView.delegate = self
buttonClicked.tableView.dataSource = self
}
#IBAction func addMoneyButtonClicked() {
buttonClickedWebserviceCall()
actionAlertViewController.actionType = ActionAlertType.ADD_MONEY
present(self.view.actionAlertPopup(alertVC: actionAlertViewController), animated: animated, completion: nil)
}
func buttonClickedWebserviceCall(){
let params: NSDictionary = ["langId" : “1”, "countryId" : “1”]
callingWebservice().dataTaskWithPostRequest(urlrequest: URL_BUTTONCLICKED viewcontroller: self, params: params) { (result, status) in
let response : NSDictionary = result as! NSDictionary
let status = response.value(forKey: "httpCode") as! NSNumber
if status == 200{
DispatchQueue.main.async {
self.tableviewDisplayArray= (response.value(forKey: “response”) as? NSArray)!
print(self.tableviewDisplayArray)
self.buttonClicked.tableView.reloadData()
}
}
else{
DispatchQueue.main.async {
}
}
}
}//method close
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if tableView == buttonClicked.tableView {
return tableviewDisplayArray.count
}
else{
return 5
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if tableView == buttonClicked.tableView {
return 30.0
}
else{
return 75.0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if tableView == buttonClicked.tableView {
let cell = buttonClicked.tableView.dequeueReusableCell(withIdentifier: "tableViewDispCell", for: indexPath) as! TableViewDisplayCell
let storedArray = self.tableviewDisplayArray.object(at: indexPath.row) as! NSDictionary
print(storedArray)
return cell
}
else{
let cell = tableView.dequeueReusableCell(withIdentifier: “normalCell”, for: indexPath) as! NormalCell
return cell
}
}
//Notice that tableView delegate methods should be in your ViewController class because that is the 'self' here so delegate and datasource is the ViewController class
//buttonClicked.tableView.delegate = self
//buttonClicked.tableView.dataSource = self
//as you write this the tableview looks for data in this ViewController class.
//Extensions are meant for another purpose.
}
//class close
I'm trying to set the a string depending on which cell in a tableView is clicked. The BlueLineTableViewController is the one which should capture the user's click.
import UIKit
class BlueLineTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return bluelinestations.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "bluelinecell", for: indexPath)
let station = bluelinestations[indexPath.row]
cell.textLabel?.text = station.name
cell.imageView?.image = UIImage(named: station.image)
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let row = indexPath.row
if row == 0 {
BlueBelmontTableViewController().feed = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=mykey&mapid=40890&outputType=JSON"
}
if row == 1 {
BlueBelmontTableViewController().feed="http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=mykey&mapid=40820&outputType=JSON"
}
}
The BlueBelmontTableViewController's feed variable should change/be set to another url depending on which cell is clicked in the BlueLineTableViewController.
import UIKit
class BlueBelmontTableViewController: UITableViewController {
class Destinations {
var destination: String = ""
var time: String = ""
}
var feed = ""
var dataAvailable = false
var records = [Destinations]()
override func viewDidLoad() {
super.viewDidLoad()
parseData()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
for r in records {
r.time = ""
r.destination = ""
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataAvailable ? records.count : 15
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if (dataAvailable) {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
let destinationRow = records[indexPath.row]
cell.textLabel?.text = destinationRow.destination
cell.detailTextLabel?.text = destinationRow.time
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "PlaceholderCell", for: indexPath)
return cell
}
}
func parseData() {
guard let feedURL = URL(string: feed) else {
return
}
let request = URLRequest(url: feedURL)
let task = URLSession.shared.dataTask(with: request) {(data, response, error) in
if error != nil
{
print("Error")
}
else {
if let content = data {
do {
let json = try JSONSerialization.jsonObject(with: content, options: []) as? [String:Any] ?? [:]
print(json)
if let ctattimetable = json["ctatt"] as? [String:Any] {
if let estArrivalTime = ctattimetable["eta"] as? [[String:Any]] {
for item in estArrivalTime{
if let headingTowards = item["destNm"] as? String,
let arrivalTime = item["arrT"] as? String {
let record = Destinations()
record.destination = headingTowards
record.time = arrivalTime
self.records.append(record)
}
self.dataAvailable = true
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
}
catch {
}
}
}
}
task.resume()
}
}
I've tried setting the url in the didSelectRowAt method depending on the indexPath.row as can be seen in BlueLineTableViewController, but it does not seem to do anything. Does anybody know how I would go about doing this?
Below is the Main.storyboard of this part of my project:
Your are not able to pass value because you are setting feed property to the completely new instance of BlueBelmontTableViewController not the one that is added in navigation stack using your segue that you have created from your UITableViewCell to BlueBelmontTableViewController.
What you need to do is override prepareForSegue in your BlueLineTableViewController to pass your value to BlueBelmontTableViewController.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! BlueBelmontTableViewController
if let indexPath = self.tableView.indexPathForSelectedRow {
if indexPath.row == 0 {
vc.feed = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=mykey&mapid=40890&outputType=JSON"
}
if indexPath.row == 1 {
vc.feed = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=mykey&mapid=40820&outputType=JSON"
}
}
}
instead of
BlueBelmontTableViewController().feed = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=mykey&mapid=40890&outputType=JSON"
use
self.feed = "http://lapi.transitchicago.com/api/1.0/ttarrivals.aspx?key=mykey&mapid=40890&outputType=JSON"
beacause BlueBelmontTableViewController() is initialing new instance of BlueBelmontTableViewController and you want to change the instance you already have so you should use self instead of creating new instance.
This is for a custom UITableView. I'd like to return a prototype cell based on what's on the stack, represented by the array MentionItemsStack. Is there anything conceptually wrong with this piece of code? Nothing appears on the custom UITableView.
class MentionsTableViewController: UITableViewController {
var mentions: Tweet? {
didSet {
placeMentionItemsInStack()
}
}
var MentionItemsStack = [String?]()
private struct Storyboard {
static let imagesCellReuseIdentifier = "imagesCell"
static let urlsCellReuseIdentifier = "urlsCell"
static let hashtagsCellResuseIdentifier = "hashtagsCell"
static let usersCellReuseIdentifier = "userMentionsCell"
static let emptyCellReuseIdentifier = "emptyCell"
}
override func viewDidLoad() {
super.viewDidLoad()
}
func placeMentionItemsInStack() {
if !mentions!.userMentions.isEmpty {
MentionItemsStack.append("Users")
}
if !mentions!.hashtags.isEmpty {
MentionItemsStack.append("Hashtags")
}
if !mentions!.urls.isEmpty {
MentionItemsStack.append("URLs")
}
if !mentions!.media.isEmpty {
MentionItemsStack.append("Images")
}
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return MentionItemsStack.count
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1 // returned 1 for now?
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let emptyCell = tableView.dequeueReusableCellWithIdentifier(Storyboard.emptyCellReuseIdentifier, forIndexPath: indexPath) as EmptyTableViewCell
if !MentionItemsStack.isEmpty {
if let identifier = MentionItemsStack.removeLast() {
switch identifier {
case "Users":
let userMentionCell = tableView.dequeueReusableCellWithIdentifier(Storyboard.usersCellReuseIdentifier, forIndexPath: indexPath) as UsersTableViewCell
userMentionCell.tweet = mentions
return userMentionCell
case "Hashtags":
let hashtagCell = tableView.dequeueReusableCellWithIdentifier(Storyboard.hashtagsCellResuseIdentifier, forIndexPath: indexPath) as HashtagsTableViewCell
hashtagCell.tweet = mentions
return hashtagCell
case "URLs":
let urlCell = tableView.dequeueReusableCellWithIdentifier(Storyboard.urlsCellReuseIdentifier, forIndexPath: indexPath) as URLsTableViewCell
urlCell.tweet = mentions
return urlCell
case "Images":
let imageCell = tableView.dequeueReusableCellWithIdentifier(Storyboard.imagesCellReuseIdentifier, forIndexPath: indexPath) as ImagesTableViewCell
imageCell.tweet = mentions
return imageCell
default: break
}
}
}
return emptyCell // arbitrary return statement
}
}