having some issue with tableview. The JSon content is in the right order but when i update the table thats when the problem starts. This is how i update the table:
var cats = [Categories]()
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
cats.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "CategoryTableViewCell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! CategoryTableViewCell
print(cats[indexPath.row].categoryName)
cell.nameLabel.text = cats[indexPath.row].categoryName
cell.subNameLabel.text = cats[indexPath.row].appShortDesc
return cell
}
When i print the JSon it shows the correct order just the tableview keeps doing what it feels like.
Related
Populating the UITableView first from JSON file. Everything seems to be loading and there are no crashes but the table cells do not contain all of the data. They are cut off at the bottom.
HeroViewController
class HeroViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, WCSessionDelegate {
// code
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return messageObject.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let tableCell : HeroTableViewCell = tableView.dequeueReusableCell(withIdentifier: "MessageCell") as? HeroTableViewCell ?? HeroTableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "MessageCell")
let row = indexPath.row
let rowObj = messageObject[row]
tableCell.one.text = rowObj.title as String?
tableCell.two.text = rowObj.speaker as String?
tableCell.three.text = rowObj.from as String?
tableCell.four.text = rowObj.to as String?
return tableCell
}
}
your cells don't seem to have enough height. you can set it by over-ridding this method:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100; //return the cell height you desire
}
I am currently using two prototype cells to have my collectionView in top cell moving horizontally while all other cells moves vertical. It's still short one cell count at the bottom and I can't seem to figure out why.
This is the code. Can you point out where the issue is please?
//Mark:- Data arrays
var dataArray: [String] = ["c1","c2","c3","c4","c5"]
var cellArray: [String] = ["10","11","12","13","14","15"]
//Mark:- UITableView Delegate
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell") as! MainTableViewCell
return cell
} else {
let cell2 = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell
cell2.cellImage.image = UIImage(named: cellArray[indexPath.row])
return cell2
}
}
//Mark:- UICollectionView Delegate
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return dataArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "InsideCollectionViewCell", for: indexPath) as! InsideCollectionViewCell
cell.myImage.image = UIImage(named: dataArray[indexPath.row])
return cell
}
It seems you want your table view to contain the values in your cellArray array plus one extra special row at index 0.
In order to do this you need to indicate that there is an extra row and your indexing needs to account for the extra row.
But a simpler approach is to use multiple sections in your table view. Use section 0 for the extra special row and use section 1 for the values in your cellArray.
func numberOfSections(in tableView: UITableView) -> Int {
return 2
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return section == 0 ? 1 : cellArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell") as! MainTableViewCell
return cell
} else {
let cell2 = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell
cell2.cellImage.image = UIImage(named: cellArray[indexPath.row])
return cell2
}
}
Make sure you adjust for the use of multiple sections in any other table view method you may implement (such as didSelectRowAt, etc.).
For the sake of comparison, here is how you would need to change your code if you want all of the rows in one section:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellArray.count + 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell") as! MainTableViewCell
return cell
} else {
let cell2 = tableView.dequeueReusableCell(withIdentifier: "CustomTableViewCell") as! CustomTableViewCell
cell2.cellImage.image = UIImage(named: cellArray[indexPath.row - 1])
return cell2
}
}
I want to generate cell dynamically according to the number of rows in a Dictionary. On view load, the dictionary is null and is binded on the road.
The dictionary is well binded with three key-values, but when I want to create cell according to dictionary values and keys always creates three rows with the last item in the dictionary.
I can't figure it out why.
This is my code:
var peripherals = [String:String]()
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print(peripherals.count)
return peripherals.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)
for (peripheralDeviceUUID,peripheralDeviceName) in peripherals {
cell.textLabel?.text = "\(indexPath) \(peripheralDeviceName) : \(peripheralDeviceUUID)"
}
return cell
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Section \(section)"
}
Firt make arrays of keys and values from your dictionary.
let dict = ["a": "first", "b": "second", "c": "third"]
let arrayKeys = Array(dict.keys)
let arrayValues = Array(dict.values)
then use those arrays in cellForRow:
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)
cell.textLabel?.text = "\(indexPath.row) \(arrayValues[indexPath.row]) : \(arrayKeys[indexPath.row])"
return cell
}
I have the following class model for the items. I am displaying itemsName,itemprice in sections. And Addonname and AdddonPrice in cell based on the index.. the number of sections and the number of cells are coming correctly from the class modal. sections data are also working fine. The problem is to display addon data into customTableCell.
class Cart{
var itemID:AnyObject?
var itemName:AnyObject?
var itemPrice:AnyObject?
var cartAddon:[AnyObject?]
init(itemID:AnyObject?,itemName:AnyObject?,itemPrice:AnyObject?,cartAddon:[AnyObject?]){
self.itemID = itemID
self.itemName = itemName
self.itemPrice = itemPrice
self.cartAddon = cartAddon
}
}
class CartAddon {
var addonID:Int?
var addonName:String?
var addonPrice:Double?
init(addonID:Int?,addonName:String?, addonPrice:Double?){
self.addonID = addonID
self.addonName = addonName
self.addonPrice = addonPrice
}
}
Tableview code
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return cartArray.count
}
func tableView(tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 50
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableCellWithIdentifier("cartheadercell") as! cartHeaderCell
header.ItemnameLbl.text = cartArray[section].itemName as? String
header.ItemPriceLbl.text = (cartArray[section].itemPrice as! String)
return header
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cartArray[section].cartAddon.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 30
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cartcell", forIndexPath: indexPath) as! AddonCell
//error in this part
let cart: Cart = cartArray[indexPath.row].cartAddon[indexPath.row]
if let name = cart.cartAddon[indexPath.row]!["AddonName"]{
cell.AddonNameLbl.text = (name as! String)
}
cell.AddonPriceLbl.text = "£ 0.0"
return cell
}
The data is coming in this form which I have to display is:
Optional(9 Inch Thin & Crispy Margarita)
Optional(£3.40)
Optional(1749)
[Optional(Chicos_Pizza.CartAddon), Optional(Chicos_Pizza.CartAddon), Optional(Chicos_Pizza.CartAddon), Optional(Chicos_Pizza.CartAddon)]
The problem is you need to use indexPath.section to access the Cart object from array not indexParh.row, so change your cellForRowAtIndexPath code like this.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cartcell", forIndexPath: indexPath) as! AddonCell
if let cartAddon = cartArray[indexPath.section].cartAddon[indexPath.row] as? CartAddon, let name = cartAddon.addonName {
cell.AddonNameLbl.text = name
}
else {
cell.AddonNameLbl.text = "Set here some default value or blank string"
}
cell.AddonPriceLbl.text = "£ 0.0"
return cell
}
I have a problem with UITableView in UIViewController.My table shows some row that I don't know why. It should be show all of row.
class TableViewController: UIViewController,UITableViewDelegate, UITableViewDataSource {
let cellIdentifier = "testCell"
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 20
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! TableViewCell
cell.lableName.text = "Hello man"
return cell
}
}
if you want hello man in 20 row.(All row)in cell for row at index path method add this line
cell.labelname.text = "Hello Man"
Please check your tableview delegate and datasource.
BTW,set right constraints.