I will be using a dynamic number of cells and resizing the height of the table on that basis. I could work out how to do this myself by I can't seem to resize the table. Whether I use 100 cells or 10 the view is set at the same height.
class generalTableViewController: UITableViewController {
override func viewWillAppear(_ animated: Bool) {
tableView.rowHeight = UITableViewAutomaticDimension
// self.tableView.contentSize.height = 2000
// self.tableView.frame.size.height = 2000
}
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1000
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
return cell
}
}
UITableViewControllers generally take over the entire view. To occupy only a portion of the view with a UITableView use a UIViewController.
You should use a UIViewController subclass rather than a subclass of UITableViewController to manage a table view if the view to be managed is composed of multiple subviews, only one of which is a table view. The default behavior of the UITableViewController class is to make the table view fill the screen between the navigation bar and the tab bar (if either are present).
See here for Apple guidance. You basically just need to take care of some of the things the controller would normally handle for you.
Related
I am currently making a golf round tracker that displays your rounds in a table view with the cells being xibs. When I add one round, it appears fine on the table view, but when I add another round it adds that cell and doubles the cells. Here is a picture of what happens: https://i.stack.imgur.com/SOgN4.png. Here is my code:
class RoundDisplay: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
arrayOfCellData.append(roundData(id : arrayOfCellData.count + 1, date : datePlayedFC, course : currentCourse, score : String(score1234) ))
tableView.reloadData()
print(arrayOfCellData)
}
override func numberOfSections(in tableView: UITableView) -> Int {
//Shows how many cells it should display; number of current cells
return arrayOfCellData.count
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
//Shows how many cells it should display; number of current cells
return arrayOfCellData.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//Defines what xib to use for cells
let cell = Bundle.main.loadNibNamed("TableViewCell", owner: self, options: nil)?.first as! TableViewCell
//Adds array data to each cell
cell.DateLbl.text = arrayOfCellData[indexPath.row].date
cell.CourseName.text = arrayOfCellData[indexPath.row].course
cell.ScoreLbl.text = arrayOfCellData[indexPath.row].score
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
//Shows how high to make cells; height of xib
return 68
}
}
The number of sections function needs to be set to one. I realized that it decided how many cells it should show for the data from the array. Which means if you have the function set as I did, for instance, if you had two sets of data in the array, it would put two cells for each data. Thanks to #Magnas and #vacawama for the help!
Try to auto resizing two labels align vertically within a cell, is that possible?
In the storyboard, we have to set autolayout, since I want to auto resizing two labels(one is on top of another).
I can't set each height so that the storyboard doesn't know what is the height of these two labels so it shows an error for autolayout.
if I click "Add Missing Constraints" button it will add a height for "subtitle"
or I can set height for "title" rather than "subtitle",
or make "title" equal to "subtitle" it will still accept.
here is the result:
A workaround will be separate these two to a different cell.
Any better solution? or am I just simply forget to do anything?
P.S. I'm using Xcode 8 with iOS 10.3.
Try setting out Proper constraints for both the labels-:
Storyboard-:
Constraints for first label-:
Constraints for second label-:
Controller class(use Automatic Dimensions for table view)-:
class ViewController1: UIViewController {
var dataForTableView:[ProductImage]?
#IBOutlet weak var secondTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
secondTable.estimatedRowHeight = 96
secondTable.rowHeight = UITableViewAutomaticDimension
// CHECK FOR DATA
//print(dataForTableView?[0].url as Any)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController1 : UITableViewDelegate,UITableViewDataSource{
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
let cell = tableView.dequeueReusableCell(withIdentifier: "cell1") as! testingCell2
cell.backgroundColor = UIColor.blue;
cell.secondLabel.text = "agfhjsgfhjsgdshgfhjsfjhvhssajs hjfbvhjfbvjhfgafgfhlgkaghkfakfhkflbvhfbvhfvbhfv ah fvfhbvfjhvbfhdavhfvhv"
return cell
}
// Number of sections in table
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}// Default is 1 if not implemented
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
return UITableViewAutomaticDimension
}
}
Output-:
I hope that is what you looking for, let me know if any issues.Thanks
I know my question is simple and I know many question on that topic but I can't find any solution to it.
I have simple table view only have the label for 100 rows. When I scroll the memory is increasing every time I scroll using the instrument tool in the Xcode although I use the dequeuing cell as apple documentation my question why the cells not releasing from memory (free from memory) every time I scroll it's creating new cell.
The following my code:
import UIKit
class ViewController: UIViewController ,UITableViewDataSource,UITableViewDelegate{
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 100
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = "Table View Cell! "
return cell
}
}
You can check the instruments tools and you can see the memory increasing every time you scroll.
Please, how can I release cells from memory when I'm scrolling?
Thanks a lot.
Simply you can deallocate memory from view controller's table view cell
When Back press clicked
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if self.isMovingFromParent {
AllProductList.removeAll()
self.view = nil
}
}
deinit {
print("deinit called")
}
Remove all array data model
Remove parent view
I created a custom UITableViewHeaderFooterView, and the tableview's background colour is white.
self.contentView.backgroundColor = UIColor.clearColor()
However, the section header's background always appears like gray. How can I remove the gray background??
Since I have override the drawRect func of the UITableView, so I want something to appear behind the header view.
I have tried the following:
a) Change the UITableView style to Grouped, the problem goes, but the header cannot glued on top of the table.
b) Use section header title instead of header view
func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String?
the header's background is transparent, but I have multiple labels.
Can anyone help me to figure this out?
Thanks to #Omkar, the correct way is set
cell.layer.backgroundColor = UIColor.clearColor().CGColor
You need to set the background colour of the content view to clear color and at the same time you also need to set the background color of the tableView cell to clear color. Place this in your viewForHeaderInSection method. You will then be able to see the color set to the tableView.
YourCell.contentView.backgroundColor = UIColor.clearColor()
YourCell.backgroundColor = UIColor.clearColor()
Please find the attached image for my code and also the table view whose style is plain style in storyboard. And i have also added the image of how it looks after running
Thanks
This is my ViewController code, the only difference is I use headerfootview subclass. I set the tableview's background to blue, and if i pull down a little bit, you will see there looks like a mask on header.
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.dataSource = self
tableView.delegate = self
tableView.registerClass(UITableViewHeaderFooterView.self, forHeaderFooterViewReuseIdentifier: "HeaderCell")
// 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 numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 4
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath)
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let cell = tableView.dequeueReusableHeaderFooterViewWithIdentifier("HeaderCell") as UITableViewHeaderFooterView!
cell!.textLabel?.text = "Header"
cell!.contentView.backgroundColor = UIColor.clearColor()
cell.backgroundColor = UIColor.clearColor()
return cell
}
}
i have been busting my brain trying to figure out how this works, but i can't seem to get it. i have tried using other tutorials, but with the many beta releases, everything keeps changing. i am fairly new to IOS development, so i'm kind of struggling.
in storyboard i have UITableView, which contains a cell with the identifier "myCell".
here's what i have so far. when i run the IOS simulator, nothing is presented on the table view.
any suggestions on how to fix this?
class ViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel?.text = "Cell #: \(indexPath.row)" // display the row number
return cell
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10; // testing out with 10 cells
}
}
Add the function
optional func numberOfSectionsInTableView(tableView: UITableView) -> Int
and return the number of sections you want.
You should make sure in the storyboard your UITableViewController has the class ViewController like so:
and that ViewController is both the delegate and datasource of the UITableViewController like so (Referencing Outlets):
You should also check that your UITableViewController is set to initialViewController if you don't see any lines at all (check the one at the bottom).