Even though i looked at other threads containing the same question, i have no clue what so ever, why it says this.
My code looks like this:
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, HomeModelProtocal {
#IBOutlet weak var listTableView: UITableView!
var feedItems: NSArray = NSArray()
var selectedLocation : Parsexml = Parsexml()
override func viewDidLoad() {
super.viewDidLoad()
//set delegates and initialize homeModel
self.listTableView.delegate = self
self.listTableView.dataSource = self
let homeModel = HomeModel()
homeModel.delegate = self
homeModel.downloadItems()
func itemsDownloaded(items: NSArray) {
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of feed items
return feedItems.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> 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
}
I have tried to use the suggested code, but no luck.
In Swift 3
numberOfRowsInSection is:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
and cellForRowAt indexPath is:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
You can easily find out yourself: Comment out a whole method and retype the first letters tableV and see what code completion suggests.
Even in Swift 2 the signature of cellForRowAtIndexPath is wrong. All parameters are non-optional values.
Related
When I run the app only numberOfRowsInSection() method is called. So i place the certain number of break point to the every method but I found that numberOfRowsInSection() is called 4-5 times rest of the method is not get called. Here is my code.
import UIKit
var arrSearch = [[String:Any]]()
class RecentSearchViewController: CustomNavigationViewController {
#IBOutlet var tblSearch: UITableView!
var searchBusVC:SearchBusViewController?
override func viewDidLoad() {
super.viewDidLoad()
if let arr = UserDefault["SearchTo"]{
arrSearch.append(arr as! [String:Any])
}
tblSearch.reloadData()
}
extension RecentSearchViewController: UITableViewDataSource , UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return arrSearch.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 200
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "RecentCell") as! RecentCell
cell.selectionStyle = .none
let dict = arrSearch[indexPath.row]
cell.lblFrom.text = (dict["From"] as! String)
cell.lblTo.text = (dict["To"] as! String)
let strDate = Utill.getStringFromDate("EEE MMM dd ,yyyy", date: (dict["fromdate"] as! Date))
cell.lblDate.text = strDate
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let searchvc = self.searchBusVC {
searchvc.setRecentSearch(dict: arrSearch[indexPath.row])
}
self.navigationController?.popViewController(animated: true)
}
}
class RecentCell: UITableViewCell {
#IBOutlet var lblFrom: UILabel!
#IBOutlet var lblTo: UILabel!
#IBOutlet var lblDate: UILabel!
}
I tried many times but it's not going to work for me. Even in console there is no error is shown. Is there any issue with the code?
If numberOfRowsInSection is called, but no other UITableViewDataSource methods are, that must mean that numberOfRowsInSection returns 0. And that must mean that arrSearch is empty. Also, this part of code
if let arr = UserDefault["SearchTo"] {
arrSearch.append(arr as! [String:Any])
}
Should be rewritten. Since you are casting the result of subscript, you can do that as a part of if let, which would also make it safer if the result isn't of [String:Any] type
if let arr = UserDefault["SearchTo"] as? [String:Any] {
arrSearch.append(arr)
}
In short, apparently UserDefault["SearchTo"] returns nil
import UIKit
import CoreBluetooth
class ScaningViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
var centralManager: CBCentralManager?
var peripheralDevice: CBPeripheral?
var devicesArray: [String] = []
var refreshControl = UIRefreshControl()
var selectedDevice: String?
#IBOutlet weak var DeviceListTableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return devicesArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? DeviceListTableViewCell
cell?.deviceName.text = devicesArray[indexPath.item]
return cell!
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectedDevice = devicesArray[indexPath.item]
print(selectedDevice!)
centralManager?.stopScan()
self.decodePeripheralState(peripheralState: selectedDevice.state)
self.peripheralDevice = selectedDevice
self.peripheralDevice?.delegate = self
print(self.peripheralDevice!)
self.centralManager!.connect(self.peripheralDevice!)
}
In func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) method when I select a row that time i get indexpath.item in string but I want this selectedDevice in CBPeripheral type.
Please give me any solution to do this.
Thanks in advance...
i have a tableview in a viewcontroller and because i need to reuse most of the code for another table i created an extra class:
class StatisticsViewDelegate: NSObject, UITableViewDelegate, UITableViewDataSource {
var defaultList:[String]
var infolist:[String] = []
var tableView:UITableView
var controller:UIViewController?
init(defaultList:[String], view:UITableView, controller:UIViewController?) {
self.defaultList = defaultList
self.controller = controller
tableView = view
super.init()
tableView.delegate = self
tableView.dataSource = self
loadTable()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return infolist.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "infocell", for: indexPath) as! TableViewCell
// [fill cell]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// [...]
}
func loadTable() {
DispatchQueue.global(qos: .userInitiated).async {
//[...]
// in this case:
self.infolist = self.defaultList
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
and in my UITViewController in the viewDidLoad():
delegate = StatisticsViewDelegate(defaultList: defaultList, view: tableView, controller:self)
delegate is a member of the ViewController
now when i run it, the function func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) never gets called. The func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) gets called however(before and after the reload) and returns the correct number(in my case 4). The TableView isn't visible at all. Where is my error?
Maybe you can use the subclassing strategy to resolve your problem. There are many reference passed to your class and if you forgot to clean that up you will be have memory leaks in your hand. So I'll suggest the simple example as below. You can modify as you like and let me know if that was what you are after. If not please pardon me.
//This will be parent class that will handle all table methods, so you need to write only once the delegates and stuffs
class MyCommonTableController: UITableViewController {
var infoList = [String]()
// MARK: - TableView Delegate and Datsource Impl
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return infoList.count
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 55.0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = infoList[indexPath.row]
return cell
}
}
The first class that is directly subclassing the from above MyCommonTableController
//In here we just have to know the data and set the infoList from parent
class TheTableViewController: MyCommonTableController {
let defaultList = ["Data1","Data2","Data3"] //....etc
override func viewDidLoad() {
super.viewDidLoad()
//this is were I will set those
infoList = defaultList
//reload the table
tableView.reloadData()
}
}
The second class that is directly subclassing the from above MyCommonTableController. Same process goes here
class TheSecondTableViewController: MyCommonTableController {
let defaultList = ["List1","List2","List3"] //....etc
override func viewDidLoad() {
super.viewDidLoad()
//this is were I will set those
infoList = defaultList
//reload the table
tableView.reloadData()
}
}
And now you are not repeating and table methods. You can also get the reference of table and use in your norma table view
#IBOutlet weak var theTable: UITableView!
let defaultList = ["List1","List2","List3"] //....etc
let commonTable = MyCommonTableController()
// MARK: - LifeCycle
override func viewDidLoad() {
super.viewDidLoad()
commonTable.infoList = defaultList
commonTable.tableView = theTable
}
In my code I have an issue that reads "Value of type 'NSManagedObject' has no member 'allObjects' ". What would I need to include in my code to make the allObjects reference work? I am watching this tutorial on coding this simple app that lists jokes using table views. Thanks in advance!
import UIKit
class jokesViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet var tableView: UITableView!
var collection: Collection?
var jokes = [Joke]()
override func viewDidLoad() {
super.viewDidLoad()
self.jokes = self.collection?.jokes?.allObjects as! [Joke]
self.tableView.delegate = self
self.tableView.dataSource = self
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.jokes.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let joke = self.jokes[indexPath.row]
cell.textLabel?.text = joke.title
return cell
}
}
For a project, I've three UITableViews in an UITabbarController. The initial view loads the tableview correctly, but when I tap on the second tab, the table loads the right amount of cells and the right cell class, but don't show the content on it.
I logged the method tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) and every cell gets the right string values.
This is the code I use:
import UIKit
import RealmSwift
class Hapjes: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tabel: UITableView!
let realm = try! Realm()
let productArray = try! Realm().objects(Product).filter("categorie = 1")
override func viewDidLoad() {
super.viewDidLoad()
tabel.dataSource = self
tabel.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Hapjes"
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return productArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ProductenCellHapjes", forIndexPath: indexPath) as! ProductenCell
var object = productArray[indexPath.row]
cell.label.text = object.valueForKey("productNaam") as! String
cell.plaatje.image = UIImage(named: "1449032338_news.png")
let tmp = object.valueForKey("productNaam") as! String
print("Hapje: \(tmp)")
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
}
}
ProductenCell.swift:
import UIKit
class ProductenCell: UITableViewCell {
#IBOutlet weak var plaatje: UIImageView!
#IBOutlet weak var label: UILabel!
}
This is the screenshot of the UI how it goes now:
http://i.stack.imgur.com/Gx0Jw.png
Thanks for your help.