UITableViewVibrantCell on UITableView - tableview

I would like to know if it was possible to use UITableViewVibrantCell on a UITableView (not controller). I can't figure out how to do it.
I'm doing :
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = self.tableView(tableView, cellForRowAt: indexPath) as! UITableViewVibrantCell
cell.blurEffectStyle = SideMenuManager.default.menuBlurEffectStyle
return cell
}
My UIViewController inherit from UITableViewDataSource and UITableViewDelegate.
Thank you for your help :)

Related

Opening two times when I move in a new viewController

I am using the following method to move between viewControllers:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "segueJokeDetail", sender: self)
}
That's the capture from the storyboard showing segueId
If you do a segue from a UITableViewCell in interface builder, you don’t need to do it in the UITableView delegate as well.

autoresizing height of UiTableviewcell height with content

I would like to update the content of my custom cell with his height so I wrote this code :
extension MyViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
}
in my customCellTableView xib i added the top,right,left,bottom constraint to all the labels and also the numberOflines properties to 0
but when t run the app i got a cell of 250 height to all the cells which is very abnormal. I work on Xcode 9 and swift 4.
please if someone can help me,

swift 3 change uitableviewcell after click uitableviewcell from another view controller?

I am trying to make my tableview cell from viewcontroller 1 change when the user click on a cell from tableview cell from viewcontroller 2. I know that I have to work with the function
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
however, I am not sure how I would accomplish what I wanted.
Here's my code:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let indexPath = changeHotelTableView.indexPathForSelectedRow;
let currentCell = changeHotelTableView.cellForRow(at: indexPath!) as! ChangeHotelTableViewCell
var valueToPass = currentCell.textLabel?.text
}
I want to get the valueToPass variable into my cell at another' view controller; say this is called timeViewController with timeTableView and a timeTableViewCell. How will I do this?
Thanks for your help and time in advance
Perhaps you should try with Protocol.
ViewController2:
protocol UserPressTheCell: NSObjectProtocol
{
func didUserPressTheCellWith(indexPath: IndexPath, text: String)
}
class ViewController2: UIViewController
{
//declare delegate
weak var delegate: UserPressTheCell?
....
func tableview(_ tableview: UITableView, didSelectRowAt indexPath: IndexPath)
{
let indexPath = changeHotelTableView.indexPathForSelectedRow;
let currentCell = changeHotelTableView.cellForRow(at: indexPath!) as! ChangeHotelTableViewCell
self.delegate?.didUserPressTheCellWith(indexPath: indexPath, text: currentCell.textLabel?.text)
}
}
Then implement in ViewController1:
class ViewController1: UIViewController, UserPressTheCell
{
#IBOutlet weak var tableview: UITableView!
...
//MARK: UserPressTheCell
func didUserPressTheCellWith(indexPath: IndexPath, text: String)
{
let cell = self.tableView.cellForRow(at: indexPath!) as! "Your VC1's cell"
cell.textLabel?.text = text
}
}
To be sure delegate will not be nil:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
let vc2 = segue.destination as! ViewController2
vc2.delegate = self
}
or wherever you segue to ViewController2.
Maybe this is the best way to accomplish what you wanted.

UITableView with 3 custom cells but 1 different

I have a TableView and I have created custom cells for that TableView with images, labels..etc, But I want one cell to be different than the others with It's own content ( cells will get their data from a backend).
So how to create 2 different types of cells in the same TableView, and how to arrange them, for example put 1 type of cell between 2 others ?
Edit: My UITableView Class:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let myCell: CellTableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! CellTableViewCell
let otherCell: OtherCellTableViewCell = tableView.dequeueReusableCellWithIdentifier("otherCell", forIndexPath: indexPath) as! OtherCellTableViewCell
// Configure the cell...
return myCell
}
If you're using a storyboard, just drag another UITableViewCell into the table view to be used as a prototype. Give it a unique and subclass (if necessary). In -tableView:cellForRowAtIndexPath: dequeue either the cell you're currently dequeueing, or this new one.
If you're not using a storyboard you'll need to either
-registerClass:forHeaderFooterViewReuseIdentifier: or
-registerNib:forHeaderFooterViewReuseIdentifier: to make your other cell style available to the table view.
Here's a modified version of your code that should work:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if condition {
let myCell: CellTableViewCell = tableView.dequeueReusableCellWithIdentifier("myCell", forIndexPath: indexPath) as! CellTableViewCell
// Configure the cell...
return myCell
}
else {
let otherCell: OtherCellTableViewCell = tableView.dequeueReusableCellWithIdentifier("otherCell", forIndexPath: indexPath) as! OtherCellTableViewCell
// Configure the cell...
return otherCell
}
}

AnyObject is not convertible to UITableViewCell

I'm using the Beginning Swift Programming book by AppCoda and I can't seem to figure out what is wrong with this block of code.
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// 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.
}
var restaurantNames = ["Cafe Deadend", "Homei", "Teakha", "CafeLoisl", "Petite Oyster", "For Kee Restaurant", "Po's Atelier", "Bourke Street Bakery", "Haigh’s Chocolate", "Palomino Espresso", "Upstate", "Traif", "Graham Avenue Meats And Deli", "Waffle & Wolf", "Five Leaves", "Cafe Lore", "Confessional", "Barrafina", "Donostia", "Royal Oak", "CASK Pub and Kitchen"]
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Return the number of rows in the section.
return restaurantNames.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->
UITableViewCell {
let cellIdentifier = "Cell"
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
cell.textLabel.text = restaurantNames[indexPath.row]
return cell
}
}
This line is throw a 'AnyObject is not convertible to 'UITableViewCell'
let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as UITableViewCell
I'm really new to Swift and iOS programming in general so any help would be appreciated.
You need to change as to as!
var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell
Also make sure that you have added a prototype cell to your storyboard with the reusable string name "Cell"
//MARK: Tableview
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int
{
return tabledata.count
}
func tableView(tableView: UITableView!,
cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!
{
let cell:UITableViewCell = UITableViewCell(style:UITableViewCellStyle.Default, reuseIdentifier:"Cell")
cell.textLabel?.text = tabledata[indexPath.row]
return cell
}
I have used this code for tableview....You can change this according to your need.Even I'm new in swift or else I would've correct your code.

Resources