Table View Displays Below Screen - ios

My issue is that the last cell in my TableView is below the screen view and to see it you must scroll up and hold your position. At a neutral position where you dont scroll up, you cant see the last cell. Everything seemed fine until i changed the size of the cells. Here is my code:
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
//MARK : Properties
var tableView = UITableView()
var items: [String] = ["Age", "Gender", "Smoking Hx", "Occup. -Ag", "Family Hx", "Chronic Lung Disease Radiology", "Chronic Lung Disease Hx", "Nodule Border", "Nodule Location", "Satellite Lesions", "Nodule Pattern Cavity", "Nodule Size"]
var navigationBar = NavigationBar()
var gender = GenderView()
override func viewDidLoad() {
super.viewDidLoad()
//Create TableView
tableView.frame = CGRectMake(0, self.view.bounds.height * 0.097, self.view.bounds.width, self.view.bounds.height);
tableView.delegate = self
tableView.dataSource = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.view.addSubview(tableView)
//Create Navigation Bar with custom class
self.navigationBar = NavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height * 0.097))
self.view.addSubview(navigationBar)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.items.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell:UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell")! as UITableViewCell
cell.textLabel?.text = self.items[indexPath.row]
//Cell wont turn grey when selected
cell.selectionStyle = UITableViewCellSelectionStyle.None
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return self.view.bounds.height * 0.095
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected cell #\(indexPath.row)!")
}
}
The only thing i could think of causing this issue is that instead of me creating a navigation bar, i created a "navigationBar" using a custom UIView() class. I then start the table view at the bottom of the navigation bar. Any idea how to fix this?
Here is the NavigationBar.swift:
class NavigationBar: UIView {
var navigationBar = UIView()
var header = UILabel()
var lineBorder = UIView()
override init(frame: CGRect) {
super.init(frame: frame)
self.frame = frame
setUpView()
}
func setUpView(){
//Create Navigation Bar
navigationBar.frame = CGRectMake(0, 0, self.bounds.width, self.bounds.height)
navigationBar.backgroundColor = UIColor.whiteColor()
self.addSubview(navigationBar)
//Create Line Border
lineBorder.frame = CGRectMake(0, self.bounds.height, self.bounds.width, self.bounds.height * 0.005)
lineBorder.backgroundColor = UIColor.grayColor()
self.addSubview(lineBorder)
header.frame = CGRectMake(0, 0, 200, 200)
header.font = UIFont(name: "Helvetica", size: 17)
header.text = "Nodule Risk Calculator"
//header.backgroundColor = UIColor.blackColor()
self.addSubview(header)
header.translatesAutoresizingMaskIntoConstraints = false
header.centerHorizontallyTo(navigationBar, padding: 0)
header.centerVerticallyTo(navigationBar, padding: 9)
}
func hide(){
self.removeFromSuperview()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

tableView.frame = CGRectMake(0, self.view.bounds.height * 0.097, self.view.bounds.width, self.view.bounds.height);
has problem. The origin.y is not 0, and you still feed the whole height. So the table view will have some area below the screen
Try this:
tableView.frame = CGRectMake(0, self.view.bounds.height * 0.097, self.view.bounds.width,
self.view.bounds.height - self.view.bounds.height * 0.097);

self.navigationBar = NavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height * 0.097))
self.view.addSubview(navigationBar)
var yAxis : Float = self.navigationBar.frame.height
var tblHeight : Float = self.view.bounds.height - yAxis
tableView.frame = CGRectMake(0, yAxis, self.view.bounds.width, tblHeight)
self.view.addSubview(tableView)
tableView.delegate = self
tableView.dataSource = self
tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

Related

Segment Controller not scrolling properly

I am trying to implement a method to where I have a tableviewcontroll that has a header where it shows a head picture as well as another section with the User's information and Profile picture. Below the head I want there to be a segment control like twitter that will allow a user to switch between different tableviews. I want that segment control to be below the header view so when a user pulls down, the segment control moves down with the header view, when you scroll up the segment control moves up with the headerview but then sticks below the navigation bar similar to how twitter and instagram's UI works. I have implemented the code below but I am getting an issue to when I scroll up, the segment controller doesn't move but when I pull down on the screen the segment control moves down with the header view. Any Idea why it is doing this?
Here is the code
class RandomTableViewController: UITableViewController {
#IBOutlet weak var trackImage: UIImageView!
private let tableHeaderViewHeight: CGFloat = 320.0
private let tableHeaderViewCutAway: CGFloat = 0.1
var headerView: HeaderView!
var headerMaskLayer: CAShapeLayer!
override func viewDidLoad() {
super.viewDidLoad()
self.automaticallyAdjustsScrollViewInsets = false
tableView.estimatedSectionHeaderHeight = 40.0
navigationController?.navigationBar.setBackgroundImage(UIImage(), for: .default)
navigationController?.navigationBar.shadowImage = UIImage()
tableView.tableFooterView = UIView()
headerView = tableView.tableHeaderView as! HeaderView
// headerView.imageView = trackImage
tableView.tableHeaderView = nil
tableView.addSubview(headerView)
tableView.contentInset = UIEdgeInsets(top: tableHeaderViewHeight, left: 0, bottom: 0, right: 0)
tableView.contentOffset = CGPoint(x: 0, y: -tableHeaderViewHeight + 64)
//cut away header view
headerMaskLayer = CAShapeLayer()
headerMaskLayer.fillColor = UIColor.black.cgColor
headerView.layer.mask = headerMaskLayer
let effectiveHeight = tableHeaderViewHeight - tableHeaderViewCutAway/2
tableView.contentInset = UIEdgeInsets(top: effectiveHeight, left: 0, bottom: 0, right: 0)
tableView.contentOffset = CGPoint(x: 0, y: -effectiveHeight)
updateHeaderView()
}
func updateHeaderView() {
let effectiveHeight = tableHeaderViewHeight - tableHeaderViewCutAway/2
var headerRect = CGRect(x: 0, y: -effectiveHeight, width: tableView.bounds.width, height: tableHeaderViewHeight)
if tableView.contentOffset.y < -effectiveHeight {
headerRect.origin.y = tableView.contentOffset.y
headerRect.size.height = -tableView.contentOffset.y + tableHeaderViewCutAway/2
}
headerView.frame = headerRect
let path = UIBezierPath()
path.move(to: CGPoint(x: 0, y:0))
path.addLine(to: CGPoint(x: headerRect.width, y: 0))
path.addLine(to: CGPoint(x: headerRect.width, y: headerRect.height))
path.addLine(to: CGPoint(x: 0, y: headerRect.height - tableHeaderViewCutAway))
headerMaskLayer?.path = path.cgPath
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
self.tableView.decelerationRate = UIScrollView.DecelerationRate.fast
}
override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
return UITableView.automaticDimension
}
#IBAction func backButton(_ sender: Any) {
dismiss(animated: true, completion: nil)
}
}
extension RandomTableViewController {
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let v = UIView()
v.backgroundColor = .white
let segmentedControl = UISegmentedControl(frame: CGRect(x: 10, y: 5, width: tableView.frame.width - 20, height: 30))
segmentedControl.insertSegment(withTitle: "One", at: 0, animated: false)
segmentedControl.insertSegment(withTitle: "Two", at: 1, animated: false)
segmentedControl.insertSegment(withTitle: "Three", at: 2, animated: false)
v.addSubview(segmentedControl)
return v
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
100
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: UITableViewCell! = tableView.dequeueReusableCell(withIdentifier: "CellID")
cell.textLabel?.text = "\(indexPath.row)"
return cell
}
}
extension RandomTableViewController {
override func scrollViewDidScroll(_ scrollView: UIScrollView) {
updateHeaderView()
}
}
I have custom HeaderView code which is
import UIKit
class HeaderView: UIView {
#IBOutlet weak var imageView: UIImageView!
#IBOutlet weak var trackImage: UIImageView!
#IBOutlet weak var backButton: UIButton!
}
Here is what it is doing
UPDATE:
I implemented the grouped tableview and setting the estimatedSectioHeaderHeight in the viewForSectionInHeader and now the segmented control moves up and down but it is not sticking below the navigation bar when scrolling up. Also there seems to be a formatting issue now. Here is how the simulator looks
change the table style to grouped and set height of the segment view in heightForHeaderInSection.
Check whether it works.

How to create UITableview first index cell show on half banner view using Swift?

My scenario, I am trying to create UITableview with card effects and top banner. I have added It will work like stretchable tableview header. Everything I am done by using storyboard but I am stuck in showing half of tableview first index cell show on banner head. I tried multiple ideas but I didn’t completed. Provide some idea for achieve this design.
Current Output:
Expected Output:
From your demo project HERE I have made some changes to you existing code like your scrollViewDidScroll will be:
var lastContentOffset: CGFloat = 0
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
self.lastContentOffset = scrollView.contentOffset.y
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if self.lastContentOffset < scrollView.contentOffset.y {
//Up
let y = 220 - (scrollView.contentOffset.y + 220)
let height = min(max(y, 60), 220)
if height >= 128 {
imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: height + 70)
}
} else if self.lastContentOffset > scrollView.contentOffset.y {
//Down
let y = 300 - (scrollView.contentOffset.y + 300)
let height = min(max(y, 60), 400)
imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: height + 80)
}
}
With above code I have added different conditions for up and down as per your requirement and from storyboard I have change top constraint to 84 so that your table will not scroll to top.
So your full code will look like:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var tableView: UITableView!
let imageView = UIImageView()
var lastContentOffset: CGFloat = 0
override func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 50
tableView.contentInset = UIEdgeInsetsMake(220, 0, 0, 0)
tableView.backgroundColor = UIColor.darkGray
imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 300)
imageView.image = UIImage.init(named: "poster")
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
view.addSubview(imageView)
self.tableView.backgroundColor = .clear
self.view.bringSubview(toFront: tableView)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
extension ViewController: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 20
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customCell", for: indexPath) as! CustomTableViewCell
switch indexPath.row % 2 {
case 0:
cell.titleLabel.text = "Lorem Ipsum is simply dummy text ."
cell.contentView.backgroundColor = UIColor.darkGray
default:
cell.contentView.backgroundColor = UIColor.black
cell.titleLabel.text = "There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..."
cell.titleLabel.textColor = .white
}
return cell
}
func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
self.lastContentOffset = scrollView.contentOffset.y
}
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if self.lastContentOffset < scrollView.contentOffset.y {
let y = 220 - (scrollView.contentOffset.y + 220)
let height = min(max(y, 60), 220)
if height >= 128 {
imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: height + 70)
}
} else if self.lastContentOffset > scrollView.contentOffset.y {
let y = 300 - (scrollView.contentOffset.y + 300)
let height = min(max(y, 60), 400)
imageView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: height + 80)
}
}
}
And result will be:
HERE is the link for your updated project.

UITableView skipping every other cell when populating labels swift

I'm trying to create several UITableViews programmatically in the same scroll view. I'm correctly loading the tables' data into each array, but for some reason the table is populating every other cell. I added a print statement in cellForRowAt: to make sure it wasn't skipping data somehow and it doesn't seem to.
Also, didSelectRowAt: isn't being called when I click on a row, but didHighlightRowAt: is called correctly. And using that the table cells seem to load properly, it's just skipping every other cell when populating them.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cellIdentifier = ""
switch tableView {
case tableView1:
cellIdentifier = "AddOnTableViewCell1"
case tableView2:
cellIdentifier = "AddOnTableViewCell2"
case tableView3:
cellIdentifier = "AddOnTableViewCell3"
case tableView4:
cellIdentifier = "AddOnTableViewCell4"
default:
print("Too many tables")
}
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? AddOnTableViewCell else {
fatalError("Dequed cell error")
}
let titleLabel = UILabel(frame: CGRect(x: 16, y: cell.frame.minY + 10, width: view.frame.width - 30, height: 30))
titleLabel.textColor = .black
titleLabel.textAlignment = .left
switch tableView {
case tableView1:
titleLabel.text = addOnContents1[indexPath.row]
print("\(indexPath.row), \(addOnContents1[indexPath.row])")
case tableView2:
titleLabel.text = addOnContents2[indexPath.row]
print("\(indexPath.row), \(addOnContents2[indexPath.row])")
case tableView3:
titleLabel.text = addOnContents3[indexPath.row]
print("\(indexPath.row), \(addOnContents3[indexPath.row])")
case tableView4:
titleLabel.text = addOnContents4[indexPath.row]
print("\(indexPath.row), \(addOnContents4[indexPath.row])")
default:
print("Too many tables")
}
cell.addSubview(titleLabel)
return cell
}
The print statements above show that the data added correctly corresponds to the indexRow.path, i.e. 0 is item00, 1 is item01, 2 is item02, 3 is item03, ...
Then here are the other two methods mentioned.
func tableView(_ tableView: UITableView, didHighlightRowAt indexPath: IndexPath) {
switch tableView {
case tableView1:
print(addOnContents1[indexPath.row])
case tableView2:
print(addOnContents2[indexPath.row])
case tableView3:
print(addOnContents3[indexPath.row])
case tableView4:
print(addOnContents4[indexPath.row])
default:
print("Too many tables")
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Pressed")
switch tableView {
case tableView1:
print(indexPath.row)
case tableView2:
print(indexPath.row)
case tableView3:
print(indexPath.row)
case tableView4:
print(indexPath.row)
default:
print("Too many tables")
}
}
Finally, here's my code for setting up the tableViews
func loadAddOnViews() {
var count = 1
for item in addOns {
var contentSize: CGFloat = 0
switch count {
case 1:
let label = UILabel(frame: CGRect(x: 16, y: yCoordinateForNewContent, width: scrollView.frame.width - 32, height: 50))
label.text = addOns[0].title
yCoordinateForNewContent += 50
scrollView.addSubview(label)
contentSize = CGFloat(addOnContents1.count)
print("content size: \(contentSize)")
contentSize = contentSize * cellSize
tableView1 = UITableView(frame: CGRect(x: 0, y: yCoordinateForNewContent, width: scrollView.frame.width, height: contentSize))
scrollView.addSubview(tableView1)
tableView1.delegate = self
tableView1.dataSource = self
self.tableView1.register(AddOnTableViewCell.self, forCellReuseIdentifier: "AddOnTableViewCell1")
yCoordinateForNewContent += contentSize + 20
tableView1.reloadData()
case 2:
let label = UILabel(frame: CGRect(x: 16, y: yCoordinateForNewContent, width: scrollView.frame.width - 32, height: 50))
label.text = addOns[1].title
yCoordinateForNewContent += 50
scrollView.addSubview(label)
contentSize = CGFloat(addOnContents2.count)
print("content size: \(contentSize)")
contentSize = contentSize * cellSize
tableView2 = UITableView(frame: CGRect(x: 0, y: yCoordinateForNewContent, width: scrollView.frame.width, height: contentSize))
scrollView.addSubview(tableView2)
tableView2.delegate = self
tableView2.dataSource = self
self.tableView2.register(AddOnTableViewCell.self, forCellReuseIdentifier: "AddOnTableViewCell2")
yCoordinateForNewContent += contentSize + 20
tableView2.reloadData()
case 3:
let label = UILabel(frame: CGRect(x: 16, y: yCoordinateForNewContent, width: scrollView.frame.width - 32, height: 50))
label.text = addOns[2].title
yCoordinateForNewContent += 50
scrollView.addSubview(label)
contentSize = CGFloat(addOnContents3.count)
print("content size: \(contentSize)")
contentSize = contentSize * cellSize
tableView3 = UITableView(frame: CGRect(x: 0, y: yCoordinateForNewContent, width: scrollView.frame.width, height: contentSize))
scrollView.addSubview(tableView3)
tableView3.delegate = self
tableView3.dataSource = self
self.tableView3.register(AddOnTableViewCell.self, forCellReuseIdentifier: "AddOnTableViewCell3")
yCoordinateForNewContent += contentSize + 20
tableView3.reloadData()
case 4:
let label = UILabel(frame: CGRect(x: 16, y: yCoordinateForNewContent, width: scrollView.frame.width - 32, height: 50))
label.text = addOns[3].title
yCoordinateForNewContent += 50
scrollView.addSubview(label)
contentSize = CGFloat(addOnContents4.count)
print("content size: \(contentSize)")
contentSize = contentSize * cellSize
tableView4 = UITableView(frame: CGRect(x: 0, y: yCoordinateForNewContent, width: scrollView.frame.width, height: contentSize))
scrollView.addSubview(tableView4)
tableView4.delegate = self
tableView4.dataSource = self
self.tableView4.register(AddOnTableViewCell.self, forCellReuseIdentifier: "AddOnTableViewCell4")
yCoordinateForNewContent += contentSize + 20
tableView4.reloadData()
default:
print("too many tables")
}
count += 1
}
}
And here's the screen with example data:
SimulatorScreen
Any help is greatly appreciated!
Thanks #Naresh for bringing my attention to this. I had declared the label in the AddOnTableViewCell class, but I was creating a label in the cellForRowAt: method, which I was adding as a subview to the cell. I simplified the tables to a single table with an array of arrays to hold the data. I made the following change to my AddOnTableViewCell class:
class AddOnTableViewCell: UITableViewCell {
var titleLabel: UILabel!
init(frame: CGRect, title:String) {
super.init(style: .default, reuseIdentifier: "AddOnTableViewCell")
titleLabel = UILabel(frame: CGRect(x: 16 , y: self.frame.minY + 10, width: self.frame.width - 32, height: 40))
titleLabel.textColor = UIColor.black
titleLabel.text = title
addSubview(titleLabel)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
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 I simplified my cellForRowAt: method:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let title = tableContents[indexPath.section][indexPath.row]
let cell = AddOnTableViewCell(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: cellSize), title: title)
return cell
}
Thanks!

how to blur only the part of uitableview getting scrolled

I have to implement a menu just like the "example.gif" in the following library link
i'm using this same library to blur the background.
i've used contentInset so that the first three rows show up from the bottom.
Now, my problem is when i start scrolling the entire screen is blurred, whereas i want to blur the part of screen where uitableviewcells are getting scrolled. (Ultimately, the entire screen will be blurred as soon as the first cell reaches the top).
how can i achieve this. If there is any workaround without using the library, it is also welcome. Here is the code--
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate{
#IBOutlet weak var table: UITableView!
var blurView: DKLiveBlurView!
var unsortedCountryArray:[String] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let topInset = self.view.frame.height - 120
// Array to display.
let countryArray = NSLocale.ISOCountryCodes()
for countryCode in countryArray {
let displayNameString = NSLocale.currentLocale().displayNameForKey(NSLocaleCountryCode, value: countryCode)
if displayNameString != nil {
unsortedCountryArray.append(displayNameString!)
}
}
// =============setting background==============
// self.bkgView = UIImageView(frame: self.view.bounds)
// self.bkgView.image = UIImage(named: "bg1")
// self.bkgView.contentMode = .ScaleAspectFill
// self.view.addSubview(self.bkgView)
// self.blurredBkgView = UIImageView(frame: self.view.bounds)
// self.blurredBkgView.image = UIImage(named: "bg1")
// self.blurredBkgView.contentMode = .ScaleAspectFill
// self.view.addSubview(blurredBkgView)
// self.blurredBkgView.alpha = 0.0
//
// blurEffect = UIBlurEffect(style: .Light)
// visualEffectView = UIVisualEffectView(effect: blurEffect)
// visualEffectView.frame = self.blurredBkgView.bounds
// self.visualEffectView.alpha = 0.0
// self.view.addSubview(self.visualEffectView)
self.table.backgroundColor = UIColor.clearColor()
self.table.separatorColor = UIColor.clearColor()
self.table.contentInset = UIEdgeInsetsMake(topInset, 0, 0, 0)
self.table.rowHeight = 40
print("view bounds: \(self.view.bounds)\n and table bounds: \(self.table.bounds)")
self.blurView = DKLiveBlurView(frame: self.table.bounds)
self.blurView.originalImage = UIImage(named: "bg1")
self.blurView.scrollView = table
self.blurView.setBlurLevel(6.0)
self.blurView.isGlassEffectOn = true
self.table.backgroundView = self.blurView
}
func scrollViewDidScroll(scrollView: UIScrollView) {
// let height = CGFloat(scrollView.bounds.size.height)
// let position = max(scrollView.contentOffset.y, 0.0)
// let percent = min(position / height, 1.0)
// self.blurredBkgView.alpha = percent;
// print("scrollview bounds: \(scrollView.bounds)")
}
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
// cell.backgroundView = self.blurView
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return unsortedCountryArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell")!
cell.textLabel?.text = unsortedCountryArray[indexPath.row]
cell.textLabel?.textColor = UIColor.blueColor()
cell.backgroundColor = UIColor.clearColor()
cell.selectionStyle = .None
return cell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
This much code is blurring while scrolling.
To account for your content inset you need to change the frame you provide to your blurView
Eg
let contentInset = CGFloat(/*Your content inset*/)
let blurFrame = CGRect(x: 0, y: contentInset, width: tableView.frame.width, height: tableView.frame.height - contentInset)
self.blurView = DKLiveBlurView(frame: blurFrame)
EDIT: Old answer
You seem to be using bounds rather than frame for your DKLiveBlurView. This will cause you blur view to start from the top left of the screen (the origin of your view's frame)
Try:
self.blurView = DKLiveBlurView(frame: self.table.frame)
Rather than
self.blurView = DKLiveBlurView(frame: self.table.bounds)

table view with dynamic table rows and with NSLayoutConstraint’s programmatically

I implemented a table view with dynamic table rows and with NSLayoutConstraint’s programmatically. However, I encounter differences between the iPhone 5/5s/6s and iPhone 6s plus simulator.
What I basically did:
Creating a UIView (containerView) on a ScrollView (scrollView)
Creating a UITableView (infoTableView) on the containerView
Defining dynamic row heights for infoTableView
Registering a UITableViewCell (InfoTableViewCell) on infoTableView
Creating two UILabels (infoLabel and infoText) on a InfoTableViewCell
Defining horizontal and vertical constraints to infoText
When running this code below on an iPhone 5/5s/6s simulator the table row height is determined properly, and the labels are properly constrained to the table row. However, when I simulate the code on an iPhone 6 the data is not displayed correctly.
See Example.
How could this difference be explained? Are the constraints set correctly? Or am I missing some code?
A related question is about the total tableview height. I set this height in viewDidLayoutSubviews, but it is currently set to constant value since the TableHeight seems not yet been initialized here. How should the height be determined?
I hope that anybody could help.
My code:
import UIKit
class ViewController: UIViewController,UIScrollViewDelegate, UITableViewDataSource, UITableViewDelegate {
var scrollView : UIScrollView!
var containerView : UIView!
var infoTableView: UITableView!
var infoLabels = [String]()
var infoData = [String]()
override func viewDidLoad() {
super.viewDidLoad()
scrollView = UIScrollView()
scrollView.delegate = self
scrollView.contentSize = CGSizeMake(view.bounds.width, 1000)
containerView = UIView()
infoLabels = ["Label1", "Label2", "Label3"]
infoData = ["Label1Test1 Label1Test2 Label1Test3 Label1Test4 Label1Test5",
"Label3Test1 Label2Test2 Label2Test3 Label2Test4 Label2Test5",
"Label3Test1 Label3Test2 Label3Test3 Label3Test4 Label3Test5"]
// Create information TableView
infoTableView = UITableView()
infoTableView.registerClass(InfoTableViewCell.self, forCellReuseIdentifier: "Cell")
infoTableView.delegate = self
infoTableView.dataSource = self
infoTableView.estimatedRowHeight = 25
infoTableView.rowHeight = UITableViewAutomaticDimension
containerView.addSubview(infoTableView)
scrollView.addSubview(containerView)
view.addSubview(scrollView)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
scrollView.frame = view.bounds
containerView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)
// --------------------------------------------
// How to determine height of table view??????
let infoTableHeight: CGFloat = 200
//infoTableView.frame.size.height => Is nil during initialization!!!
infoTableView.frame = CGRectMake(5, 50, self.view.bounds.width - 10, infoTableHeight)
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return infoData.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let textCellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath) as! InfoTableViewCell
let infoLabel = infoLabels[indexPath.row]
let infoDataItem = infoData[indexPath.row]
cell.infoLabel.text = infoLabel
cell.infoText.text = infoDataItem
cell.selectionStyle = .None
return cell
}
}
class InfoTableViewCell: UITableViewCell {
var infoLabel = UILabel()
var infoText = UILabel()
override init(style: UITableViewCellStyle, reuseIdentifier: String!) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
infoText.textAlignment = NSTextAlignment.Left
infoText.font = UIFont.systemFontOfSize(14)
infoText.baselineAdjustment = .AlignCenters
infoText.numberOfLines = 0
infoText.translatesAutoresizingMaskIntoConstraints = false
infoText.lineBreakMode = NSLineBreakMode.ByWordWrapping
self.contentView.addSubview(infoText)
let views = ["infoText" : infoText]
let hConstraint = NSLayoutConstraint.constraintsWithVisualFormat("H:|-[infoText]-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)
self.contentView.addConstraints(hConstraint)
let vConstraint = NSLayoutConstraint.constraintsWithVisualFormat("V:|-[infoText]-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: views)
self.contentView.addConstraints(vConstraint)
infoLabel.textAlignment = NSTextAlignment.Left
infoLabel.font = UIFont.boldSystemFontOfSize(14)
infoLabel.baselineAdjustment = .AlignCenters
infoLabel.numberOfLines = 0
self.contentView.addSubview(infoLabel)
}
required init(coder decoder: NSCoder) {
super.init(coder: decoder)!
}
override func awakeFromNib() {
super.awakeFromNib()
}
override func layoutSubviews() {
super.layoutSubviews()
let width = frame.width-120
let height = frame.size.height
infoLabel.frame = CGRectMake(10, 0, 100, height)
infoText.frame = CGRectMake(110, 0, width, height)
}
}

Resources