How to make label or image dynamic in tableviewcell? - swift

I want to make tableview cell have dynamic label or image in it. I don't want to loop add subview to content view of tableview cell because it will run slow when you scroll. So can anyone help me please? Like this image:
My code is;

Without knowing the context of your code, something like this can work:
NOTE This will work on iOS <= 8
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
#IBOutlet weak var TableVC: UITableView!
let elements = ["item1 item2 item3","item1 item2", "item1"]
override func viewDidLoad() {
super.viewDidLoad()
self.TableVC.delegate = self
self.TableVC.dataSource = self
self.TableVC.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
// 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 tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return elements.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.numberOfLines = 0
cell.textLabel?.attributedText = addTextToAttribute(text: elements[indexPath.item])
return cell
}
func addTextToAttribute(text : String) -> NSMutableAttributedString {
let mutString = NSMutableAttributedString()
let s = text.components(separatedBy: .whitespaces)
let attr: [String: AnyObject] = [NSFontAttributeName: UIFont.boldSystemFont(ofSize: 20)]
for text in s {
mutString.append(NSAttributedString(string: "-" + text + "\n", attributes: attr))
}
return mutString
}
}

Related

I'm getting a error that "value of uitableviewcell has no member delegate" . is there anything that should be changed in Tviews.delegate = self

I'm getting this error message in my table view controller class
value of uitableviewcell has no member delegate
Here is an image of the error.
Is there anything that should be changed in my code?
import UIKit
class TViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
#IBOutlet weak var Tviews: UITableViewCell!
let contacts:[[String]] = [
["Elon Musk", "+1-201-3141-5926"],
["Bill Gates", "+1-202-5358-9793"],
["Tim Cook", "+1-203-2384-6264"],
["Richard Branson", "+1-204-3383-2795"],
["Jeff Bezos", "+1-205-0288-4197"],
["Warren Buffet", "+1-206-1693-9937"],
["The Zuck", "+1-207-5105-8209"],
["Carlos Slim", "+1-208-7494-4592"],
]
override func viewDidLoad() {
super.viewDidLoad()
Tviews.delegate = self
Tviews.datasource = self
// Do any additional setup after loading the view.
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contacts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableViews", for: indexPath)
print("\(#function) --- section = \(indexPath.section), row = \(indexPath.row)")
cell.textLabel?.text = contacts[indexPath.row][0]
return cell
}
You should assign your delegate and data source to your table view instance, and not your table view cell.
In order to do that, create an outlet link with your table view in the TViewController file and then edit your code like this:
import UIKit
class TViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
#IBOutlet weak var tableView: UITableView!
let contacts:[[String]] = [
["Elon Musk", "+1-201-3141-5926"],
["Bill Gates", "+1-202-5358-9793"],
["Tim Cook", "+1-203-2384-6264"],
["Richard Branson", "+1-204-3383-2795"],
["Jeff Bezos", "+1-205-0288-4197"],
["Warren Buffet", "+1-206-1693-9937"],
["The Zuck", "+1-207-5105-8209"],
["Carlos Slim", "+1-208-7494-4592"],
]
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.datasource = self
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contacts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "<your-table-view-cell-id>", for: indexPath) as! YourTableViewCell
cell.textLabel?.text = contacts[indexPath.row][0]
return cell
}
}

Disable users to scroll up in tableView?

Similar to tinder, if a user goes down, I don't want them to be able to go back upwards. How do I do this?
I tried the code below, but it keeps running an error: Index out of range.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return userList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "tableCell", for: indexPath) as! FeedTableViewCell
cell.collectionView.delegate = self
cell.collectionView.dataSource = self
//reloading the tableview
let artist: UserModel
artist = self.userList[indexPath.row]
cell.nameLabel.text = artist.name
cell.passionLabel.text = artist.passion
cell.collectionView.reloadData()
if indexPath.row == 1 {
userList.removeFirst()
}
return cell
}
Refer below code,
You need to store last scroll Content Offset of tableview, when user scroll down, refer scrollViewDidScroll method.
#IBOutlet weak var tableView: UITableView!
var lastContentOffsetY:CGFloat = 0.0
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.bounces = false
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "reuse")
self.tableView.reloadData()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 50
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "reuse")
return cell!
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if(scrollView.contentOffset.y < self.lastContentOffsetY){
self.tableView.setContentOffset(CGPoint(x:scrollView.contentOffset.x, y: self.lastContentOffsetY), animated: false)
}else{
self.lastContentOffsetY = scrollView.contentOffset.y
}
}

Table View is not reflecting change in size

//
// ViewController.swift
// practise
//
// Created by ali on 29/11/2019.
// Copyright © 2019 smartsolution. All rights reserved.
//
import UIKit
class nestedviewcontroller: UIViewController {
var maindatacells:[String]=["m1","m2","m3","m9"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
}
extension nestedviewcontroller:UITableViewDataSource,UITableViewDelegate{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
tableView.rowHeight = UITableView.automaticDimension
return maindatacells.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! maintablecell
cell.lbltitle.text = maindatacells[indexPath.row]
cell.setdata(cont: self)
return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
var height = cell.frame.height
}
func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
var heightjjj = cell.frame.height
}
}
class maintablecell:UITableViewCell,UITableViewDelegate,UITableViewDataSource{
#IBOutlet weak var tblheightconstraint:NSLayoutConstraint!
#IBOutlet weak var lbltitle:UILabel!
#IBOutlet weak var tblinside:UITableView!
var cellheight:CGFloat = 20
var parentcont:UIViewController!
override func awakeFromNib() {
super.awakeFromNib()
tblinside.delegate = self
tblinside.dataSource = self
}
func setdata(cont:UIViewController){
self.parentcont = cont
}
var datacells = ["i1","i2","i3","67"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return datacells.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = datacells[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.row == 0{
cellheight = 0
}
cellheight += cell.contentView.frame.height
if indexPath.row == datacells.count - 1{
// var tbl = self.superview as! UITableView
// tblinside.estimatedRowHeight = cellheight
// tbl.estimatedRowHeight = cellheight
tblheightconstraint.constant = cellheight
tblinside.beginUpdates()
tblinside.endUpdates()
self.setNeedsDisplay()
self.setNeedsLayout()
self.setNeedsUpdateConstraints()
self.parentcont.view.setNeedsLayout()
self.parentcont.view.setNeedsDisplay()
// var siz = self.frame.size
// //
// self.frame.size = CGSize(width: siz.width, height: siz.height + 30)
}
}
}
I am updating tableview size programmaticaly by changing its constraint. But tableview is not reflecting the change untill i refresh tableview by scrolling.I have tried many functions to update layout of main cell or tableview but i didnt find anything helping.
The tableview resize perfectly when i scroll the maintableview and the cell freshes

Retrieving the tag from a cell in row in a section [duplicate]

This question already has answers here:
Cannot assign a value of type '[(String)]' to a value of type 'String!'?
(2 answers)
Closed 4 years ago.
enter image description hereI have a tableView with a custom cell. I have added a button to this cell (I have created a UITableViewCell subclass for this cell).
My data is distributed in 3 sections and rows. I want to be able to press the button at any row and pass that row content to the next page. I was able to do all this when I did not have any sections. Here is my code:
import UIKit
class ViewController: UIViewController,UITableViewDataSource {
var menu = ["a", "b", "c"]
#IBOutlet weak var tblview: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// print(menu[section].count)
return menu.count
}
func numberOfSections(in tableView: UITableView) -> Int {
//print(menu.count)
return menu.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! AddItemClassCell
cell.textLabel?.text = menu[indexPath.row]
cell.addBtn.tag = indexPath.row
cell.addBtn.addTarget(self, action: #selector(BtnClicked), for: .touchUpInside)
return cell
}
#objc private func BtnClicked(sender: UIButton) {
let vc = self.storyboard!.instantiateViewController(withIdentifier: "NextVC") as! NextVC
vc.loadViewIfNeeded()
//vc.lb.text = menu[sender.indexPath.section][sender.indexPath.row]
vc.lb.text = menu[sender.tag]
self.navigationController?.pushViewController(vc, animated: true)
}
}
Here is the cell class:
import UIKit
class AddItemClassCell: UITableViewCell {
#IBOutlet weak var addBtn: UIButton!
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
}
}
Cannot assign value of type '[String]' to type 'String?'
If your are using a button on a cell you can use
import UIKit
struct Menu {
let title: String
let innerMenu: [String]
}
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
#IBOutlet weak var myTV: UITableView!
let menuArr = [
Menu(title: "1", innerMenu: ["a", "b", "c"]),
Menu(title: "2", innerMenu: ["d", "e", "f"]),
Menu(title: "3", innerMenu: ["g", "h", "i"]),
]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
myTV.delegate = self
myTV.dataSource = self
myTV.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return menuArr.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuArr[section].innerMenu.count
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let title = UILabel()
title.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: 50)
title.text = menuArr[section].title
return title
}
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 40
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell : MyCell! = tableView.dequeueReusableCell( withIdentifier: "MyCell") as? MyCell
cell.lbl_Title.text = menuArr[indexPath.section].innerMenu[indexPath.row]
cell.btn_Click.addTarget(self, action: #selector(BtnClicked), for: .touchUpInside)
return cell as MyCell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
}
#objc func BtnClicked(sender:UIButton!){
let view = sender.superview!
let cell = view.superview as! MyCell
let indexPath = myTV.indexPath(for: cell)
print("\(indexPath?.section) - \(indexPath?.row)")
}
}

UITableView only displays one section

I have a UITableView that is supposed to have items in sections but only the first section (Science) in the array is appearing and I'm not sure what's wrong with it. It should be displaying both sections.
My code:
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
#IBOutlet weak var tableView: UITableView!
var checked = [Bool]()
let section = ["Science", "Math"]
let items = [["Physics", "Biology", "Chemistry"], ["Algebra I", "Algebra II", "Statistics"]]
override func viewDidLoad(){
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.tableView.allowsMultipleSelection = true
}
override func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return self.items[section].count
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int{
// #warning Incomplete implementation, return the number of sections
return self.section.count
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String?{
return self.section[section]
}
//All before.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = self.items[indexPath.section][indexPath.row]
//MARK: -Checkmark and save support.
cell.accessoryType = cell.isSelected ? .checkmark : .none
cell.selectionStyle = .none // to prevent cells from being "highlighted"
return cell
}
//MARK: - Checkmark and save functions.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.cellForRow(at: indexPath)?.accessoryType = .checkmark
}
func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {
tableView.cellForRow(at: indexPath)?.accessoryType = .none
}
//MARK: - Sections
}
You made a mistake, wrong method. Let change numberOfSectionsInTableView(tableView:) to:
func numberOfSections(in tableView: UITableView) -> Int {
return self.section.count
}
In ViewDidLoad :
tableView.delegate = self
tableView.dataSource = self