Swift , Identing Table Views each row gets more indented - ios

What it looks like
I am trying to make it so each name of the characters indents more and more with each row so from left then the next row goes a little more to the right and then the next row goes a little more to the right.
I simply can not figure out why I can't indent my labels one by one. Any help is appreciated.
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var Dwarves = ["Sleepy", "Sneezy", "Bashful", "Dopy", "Grumpy", "Doc", "Happy", "Sad"]
var imagess = ["Sleepy", "Sneezy", "Bashful", "Dopy", "Grumpy", "Doc", "Happy", "Sad"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Dwarves.count
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! CustomTableViewCell
cell.CellView.layer.cornerRadius = cell.CellView.frame.height / 2
cell.Label.text = Dwarves[indexPath.row]
cell.CharcterImage.image = UIImage(named:
imagess[indexPath.row])
cell.CharcterImage.layer.cornerRadius = cell.CharcterImage.frame.height / 2
return cell
}
func tableView(_ tableView: UITableView, indentationLevelForRowAt indexPath: IndexPath.) -> Int {
}
}
I tried to use the indentationLevelforRowAt but it doesn't seem to work.
import UIKit
class CustomTableViewCell: UITableViewCell {
#IBOutlet weak var CellView: UIView!
#IBOutlet weak var CharcterImage: UIImageView!
#IBOutlet weak var Label:UILabel!
#IBOutlet weak var TableView:UITableView!
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
}
}

A primitive way to do it would be to prefix the string with spaces. Here I use an indentation level of two spaces.
cell.Label.text = "\(String(repeating: " " , count: indexPath.row*2))\(Dwarves[indexPath.row])")

indentationLevelforRowAt allows you to display the table rows in a hierarchal / tree-view type format. But you want to indent just a part of your cell. Therefore I think you should play with your constraints.
One of possible solutions is create a leading constraint for label and in cellForRowAt method write something like this:
cell.labelLeadingConstraint.constant = 5 * indexPath.row

Related

how to add header view to each of custom tableViewCells in swift

I have two custom TableViewCells. So first TableViewCell is like a recent list, it can become longer. but second cell is always stays at bottom. so i need to add UILabel that hold's secondTableViewCell. the result that i need.
import UIKit
class bagPage: UIViewController, UITableViewDelegate, UITableViewDataSource {
var itemsName: [String] = []
var itemsPhoto: [UIImage] = []
// these arrays will defined in other view controller
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row < itemsName.count{
return 165
}else{
return 50
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 6 + itemsName.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row < itemsName.count{
let cell = tableView.dequeueReusableCell(withIdentifier: "bagTableViewCell") as? bagTableViewCell
cell?.itemName.text = itemsName.last
cell?.itemPhoto.image = itemsPhoto.last
return cell!
}
if indexPath.row == itemsName.count {
let cellTwo = tableView.dequeueReusableCell(withIdentifier: "extraBagPageTableView") as? extraBagPageTableView
// here i'm hiding views in first row
cellTwo?.textLabel?.text = "These are products that u can add to your cell"
return cellTwo!
}else{
let cellTwo = tableView.dequeueReusableCell(withIdentifier: "extraBagPageTableView") as? extraBagPageTableView
return cellTwo!
}
}
}
This is another attempt at providing a solution. Is this what you are looking for?
Here is a link to the project
https://drive.google.com/file/d/1XlSF4fGiNzOqQHauiNo7TuFhKUTrFbsJ/view?usp=sharing
Let me know if you need more help
Good evening, so I followed your image and made the code follow your convention of 1 tableview section, 2 difference cells. I tried to use your naming convention. Note I had to swap around the height values, you had them wrong way round.
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
var itemsName: [String] = ["Helmet", "Gloves", "Bindings", "Goggles", "Kneepads", "Boots", "Snowboard"]
override func viewDidLoad() {
super.viewDidLoad()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1 + itemsName.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row < itemsName.count{
let bagTableViewCell = tableView.dequeueReusableCell(withIdentifier: "BagTableViewCell") as? BagTableViewCell
bagTableViewCell?.productsLabel.text = itemsName[indexPath.row]
return bagTableViewCell!
} else {
let extraBagPageTableView = tableView.dequeueReusableCell(withIdentifier: "ExtraBagPageTableView") as? ExtraBagPageTableView
return extraBagPageTableView!
}
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row < itemsName.count {
return 50
} else {
return 165
}
}
}
And the tableview cells. Please note the second cell has a bad name.
class BagTableViewCell: UITableViewCell {
#IBOutlet weak var additionalProductsLabel: UILabel!
#IBOutlet weak var productsLabel: UILabel!
#IBOutlet weak var productImage: UIImageView!
}
class ExtraBagPageTableView: UITableViewCell {
}
And altogether it looks like this
And the project can be found here. (I set the permissions correctly this time :) )
TableCellWithLabel Project

How do I use extensions to extend an object's tableview's functionality from the calling class?

Overview
I'm trying to better understand how extensions work.
In my app I have a ViewController. There I put a view of another class. In this custom class I put a bunch of buttons and a table view. I want them to display some text inside of my tableView whenever I press them.
The problem is that I want to edit some of the table view functions in order to better adjust it to my ViewController.
What I know
All I know is based on the apple documentation
What I'm doing
What I'm trying to do, I should say, is to add functionality to a custom view's function after adding an object which is of the type of my custom class to the ViewController.
This is my custom class:
class CustomClass: UIView{
#IBOutlet weak var abtn: UIButton!
#IBOutlet weak var table: UITableView!
func setupTable(){
table.delegate = self
table.dataSource = self
table.register(UITableViewCell.self, forCellReuseIdentifier: "cellId")
table.backgroundColor = UIColor.black.withAlphaComponent(0.1)
}
}
extension CustomClass: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = table.dequeueReusableCell(withIdentifier: "cellId", for: indexPath)
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("I want to add stuff here too")
}
//And more stuff that is not useful rn
}
Inside of the ViewController class I have declared a variable of type CustomClass.
#IBOutlet weak var custom: CustomClass!
In my viewDidLoad I call :
custom.setupTable()
What I need to do is creating an extension to edit the tableview that belongs to custom (the variable of type CustomClass that is inside of my ViewController).
I have no clue on how to do that.
I know how to work with extension to expand my code's functionality but I don't know how to use them to edit these other functions.
Question
How do I edit the tableview functions that belong to custom?
Ie. how would I be able to change the number of rows or to change the cell's layout from the class I call the object in?
I hope I was clear enough...
For this specific example...
Add a property to your CustomClass:
class CustomClass: UIView {
// this may be changed by the "calling class"
var numRows: Int = 10
#IBOutlet weak var abtn: UIButton!
#IBOutlet weak var table: UITableView!
func setupTable(){
table.delegate = self
table.dataSource = self
table.register(UITableViewCell.self, forCellReuseIdentifier: "cellId")
table.backgroundColor = UIColor.black.withAlphaComponent(0.1)
}
}
In your extension, use that property:
extension CustomClass: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// don't make this a hard-coded number
//return 10
return numRows
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = table.dequeueReusableCell(withIdentifier: "cellId", for: indexPath)
return cell
}
//And more stuff that is not useful rn
}
Then, in your "calling class", you can change that property:
class ExampleViewController: UIViewController {
let myView = CustomClass()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(myView)
// constraints, etc
// change the number of rows in the table in myView
myView.numRows = 20
}
}
More likely, though, you would be doing something like setting / changing the data for the table in your custom class.
Here's an example, along with showing how to use a closure to "call back" to the calling class / controller:
class CustomClass: UIView {
// this may be changed by the "calling class"
var theData: [String] = []
// closure to "call back" to the controller
var callback: ((IndexPath) -> ())?
#IBOutlet weak var abtn: UIButton!
#IBOutlet weak var table: UITableView!
func setupTable(){
table.delegate = self
table.dataSource = self
table.register(UITableViewCell.self, forCellReuseIdentifier: "cellId")
table.backgroundColor = UIColor.black.withAlphaComponent(0.1)
}
}
extension CustomClass: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return theData.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = table.dequeueReusableCell(withIdentifier: "cellId", for: indexPath)
cell.textLabel?.text = theData[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// tell the controller the cell was selected
callback?(indexPath)
}
}
class ExampleViewController: UIViewController {
let myView = CustomClass()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(myView)
// constraints, etc
// set the data in CustomClass
myView.theData = [
"First row",
"Second row",
"Third",
"Fourth",
"etc..."
]
myView.callback = { indexPath in
print("CustomClass myView told me \(indexPath) was selected!")
// do what you want
}
}
}

Table View Cells on second View

I have a TableView with cells on my first View, and it works well. But I want to click on some cell then should appear new View with new Table View & cells. But after click, I can see only TableView without any cells.
code of SecondViewController
import UIKit
import WebKit
import Kanna
import Gloss
class SecondViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource {
#IBOutlet weak var menuTable: SecondViewControllerTableViewCell!
public func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return 3
}
public func tableView(_ tableView: UITableView, cellForRowAt
indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:
"lte_secondPage", for: indexPath) as! ViewControllerTableViewCell
return cell
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
code of FirstViewController
class ViewController: UIViewController, UITableViewDelegate,
UITableViewDataSource {
let logos = ["ks", "vf", "ls"]
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return logos.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier:
"lte_firstPage", for: indexPath) as! ViewControllerTableViewCell
#code#
}
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
IndexPath) {
performSegue(withIdentifier: "segue", sender: self)
}
override func viewDidLoad() {
super.viewDidLoad()
}
Code of First Table Cell Controller
import UIKit
class ViewControllerTableViewCell: UITableViewCell {
#IBOutlet weak var logoImage: UIImageView!
#IBOutlet weak var regionQuantity: UILabel!
#IBOutlet weak var localityQuantity: UILabel!
#IBOutlet weak var BSQuantity: UILabel!
#IBOutlet weak var updateDate: UILabel!
#IBOutlet weak var namesOfBS: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
Code of Second Table Cell Controller
import UIKit
class SecondViewControllerTableViewCell: UITableViewCell {
#IBOutlet weak var label: 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
}
}
And when I tried to print something in
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { print(3) return 3 } it printed nothing and
2019-01-04 22:54:07.057626+0200 BaseStation[12620:273434] <UIView: 0x7f9f52c1e640; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x600001a62a80>>'s window is not equal to <BaseStation.SecondViewController: 0x7f9f52c239e0>'s view's window!
in debuger apeared after click onto cells in first View
I think what you are asking is .. When you click on a cell in your tableview, you navigate to a new view controller. And this new view controller has a tableview that is not being populated with any cells.
Essentially your question is.. Why is your tableview empty.
1) Have you checked if you correctly set the table views datasource?
2) Is the datasource empty?
3) Does your tableview have a visible height/width?

Edit cell outlets when editing tableview

This is my app:
When i press the Edit button (''Rediger''), i get the following:
So my question is: How do i make the round red circle and the disclosure indicator hidden while the tableview is being edited? Both the circle and the disclosure indicator are imageViews.
Here is the code for the custom cell (i edited out the unnecessary parts):
class KundeavisCell: UITableViewCell {
#IBOutlet weak var unreadImage: UIImageView!
#IBOutlet weak var disclosureIndicatorImage: UIImageView!
}
And the view controller with the table:
class KundeaviserVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var redigerOutlet: UIBarButtonItem!
var stores = ["Rema 1000", "Coop Obs", "Coop Extra"]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "KundeavisCell", for: indexPath) as! KundeavisCell
// Here i edit the cell
return cell
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return stores.count
}
#IBAction func tableViewEditingPressed(_ sender: Any) {
if tableView.isEditing {
tableView.setEditing(false, animated: true);
redigerOutlet.style = UIBarButtonItemStyle.plain;
redigerOutlet.title = "Rediger";
} else {
tableView.setEditing(true, animated: true);
redigerOutlet.title = "Ferdig";
redigerOutlet.style = UIBarButtonItemStyle.done;
}
}
func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, moveRowAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
let itemToMove = stores[sourceIndexPath.row]
stores.remove(at: sourceIndexPath.row)
stores.insert(itemToMove, at: destinationIndexPath.row)
}
func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCellEditingStyle {
return UITableViewCellEditingStyle.none
}
func tableView(_ tableView: UITableView, shouldIndentWhileEditingRowAt indexPath: IndexPath) -> Bool {
return false
}
}
I know i can indent the images to the left by setting constraints on them, but i want to hide them completely. I found a question about it: Link but unfortunately it was written 5 years ago and in Objective-C.
Any help will be highly appreciated, thanks!
That code you reference is still valid to now. You already have the Custom implementation of the UITableViewCell so now implement the SetEditing function and in the implementation hide/shows your images.
override func setEditing(_ editing: Bool, animated: Bool) {
unreadImage.isHidden = editing
disclosureIndicatorImage.isHidden = editing
}
That should work.

Custom TableViewCell Swift

Here is the code from my custom cell class:
import UIKit
class CustomOrderTableViewCell: UITableViewCell {
#IBOutlet var MealName: UILabel!
#IBOutlet var MealPrice: UILabel!
#IBOutlet var MealDescription: 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
}
#IBAction func deleteMeal(sender: AnyObject) {
}
}
Here are the table view related functions:
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return mealArray.orderedMeals.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
print("in")
var cell = tableView.dequeueReusableCellWithIdentifier("orderCell", forIndexPath: indexPath) as! CustomOrderTableViewCell
cell.MealName.text = mealArray.orderedMeals[indexPath.row].mealName
cell.MealDescription.text = mealArray.orderedMeals[indexPath.row].mealDescription
let price = NSString(format: "%.2f", mealArray.orderedMeals[indexPath.row].mealPrice) as String
cell.MealPrice.text = "R" + price
return cell
}
The problem is that nothing gets displayed in the table view and func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell is never called.
Any solutions? Thanks
If func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell is never called that means you have not connected your UITableViewDataSource in IB correctly. Make sure that you connect the dataSource property with your ViewController. Also check that all classes are set for your ViewController, TableView and the Cell itself as this another common mistake.

Resources