UITableVC No section header content shown - swift

converting my code to swift ONLY so no use of storyboards. After rewriting the code (no storyboard usage) the header information is NOT shown !!
If I change the viewForHeaderInSection return value from:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "headerId") as! ListsVCHeader
cell.fillHeader(header: listTitles[section])
return cell.contentView
}
to:
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "headerId") as! ListsVCHeader
cell.fillHeader(header: listTitles[section])
return cell // ==> removed .contectView
}
the header content is shown BUT the section header moves left performing a swipe gesture that I use to initiate a row delete. Any suggestions? Find the relevant code below.
class ListsVC: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(ListsVCHeader.self, forCellReuseIdentifier: "headerId")
// register other Cells
}
override func numberOfSections(in tableView: UITableView) -> Int {
return listTitles.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int)
-> String? {
return listTitles[section]
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableCell(withIdentifier: "headerId") as! ListsVCHeader
cell.fillHeader(header: listTitles[section])
return cell.contentView
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return HeaderSectionHeight
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
switch editingStyle {
case .delete:
// mycode
default:
break
}
}
}
the Section Header cell
class ListsVCHeader: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
// add subviews and constraints
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func fillHeader (header: String) {
name.text = header
}
}

Change your register in viewDidLoad to:
tableView.register(ListsVCHeader.self, forHeaderFooterViewReuseIdentifier: "headerId")
Your class ListsVCHeader should be changed to:
class ListsVCHeader: UITableViewHeaderFooterView
and last change the following code:
let cell = tableView.dequeueReusableCell(withIdentifier: "headerId") as! ListsVCHeader
to
let cell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerId) as! ListsVCHeader

Related

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
}
}

UITableView Data Source

I am receiving 2 errors.
The first I am confused about because everything is inside the {} so I'm not sure where I am going wrong.
"Type 'StoriesViewController' does not conform to protocol
'UITableViewDataSource' Clicking fix adds:
func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
<#code#>
}
and 2nd error
Ambiguous use of 'dequeueReusableCellWithIdentifier
I assigned the table cell the identifier "storyCell" from the main storyboard
Full code:
// creating a custom color
extension UIColor {
static var darkmodeGray = UIColor.init(red: 41/255, green: 42/255, blue: 47/255, alpha: 1)
}
class StoriesViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
<#code#>
}
#IBOutlet weak var tableView: UITableView!
let storySelection = [("WeekOne"),("WeekTwo"),("WeekThree"),("WeekFour"),("WeekFive"),("WeekSix")]
let storySelectionImages = [UIImage(named: "WeekOne"), UIImage(named: "WeekTwo"), UIImage(named: "WeekThree"), UIImage(named: "WeekFour"), UIImage(named: "WeekFive"), UIImage(named: "WeekSix")]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.view.backgroundColor = UIColor .darkmodeGray
}
//Part One
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
//Part Two
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return storySelection.count
}
//Part Three
private func tableView(tableView :UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell=tableView.dequeueReusableCellWithIdentifier("storyCell", forIndexPath: IndexPath) as! TableViewCell
cell.CoverArt.image=self.storySelectionImages[indexPath .row]
return cell
}
}
Your code is Swift 2(!) code.
The usual way to update the signature of a method is to comment it out, retype it and use code completion.
Or read the documentation, that's always a good idea.
The three methods are (numberOfSections can be omitted if there is only one section)
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return storySelection.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "storyCell", for: indexPath) as! TableViewCell
cell.CoverArt.image = self.storySelectionImages[indexPath.row]
return cell
}

dropdown tableview - Swift

I draw data from MSSQL and load this data into tableView. No problem with that. I want to make a drop down menu, but I don't know how to do it. Now all the menus are angled. How do I make tables open and close? How can I do this? How do I make a folding tableview?
class CollapsibleTableViewController: UITableViewController {
var sectionNames: [String] = []
var sectionDetay: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
makaleDetay()
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableView.automaticDimension
}
func makaleDetay(){
...
client.execute("SELECT soru_icerik FROM uzaktanptomg_db.omg_user.ie_soru", completion: { (_ results: ([Any]?)) in
... self.sectionDetay.append(String(result))
...
}
extension CollapsibleTableViewController {
override func numberOfSections(in tableView: UITableView) -> Int {
return sectionDetay.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: CollapsibleTableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell") as? CollapsibleTableViewCell ??
CollapsibleTableViewCell(style: .default, reuseIdentifier: "cell")
cell.nameLabel.text = "SSS"
cell.detailLabel.text = sectionDetay[indexPath.section]
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableView.automaticDimension
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "header") as? CollapsibleTableViewHeader ?? CollapsibleTableViewHeader(reuseIdentifier: "header")
header.titleLabel.text = sectionNames[section]
header.arrowLabel.text = ">"
header.setCollapsed(false)
header.section = section
header.delegate = self
return header
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return 44.0
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return 1.0
}
}
extension CollapsibleTableViewController: CollapsibleTableViewHeaderDelegate {
func toggleSection(_ header: CollapsibleTableViewHeader, section: Int) {
....
}
}

How to make label or image dynamic in tableviewcell?

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
}
}

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