the swift code below allows you to view a side menu my problem is this, the first element of the menu is an image (attached) now the problem lies in the fact that in the image there is a space to the left of the screen, what I want to do is remove the space so that the image is flush with the screen, and bring the top image flush with the iPhone, the result I want to obtain is the one in figure 2, as I do this?
Swift Code:
protocol MenuControllerDelegate {
func didSelectMenuItem(named: SideMenuItem)
}
enum SideMenuItem: String, CaseIterable {
//DA NASCONDERE PRIMA VOCE MENU - FUNZIONALE PER BANNER HOMEPAGE
case header = "Sito Web SynergyO2"
//BLOCCO 1 - INTRO
case home = "SynergyO2 - SO2 Business"
//BLOCCO 2 - SCOPRI SYNERGY O2
case sito = "Sito SynergyO2"
case prodotti = "Prodotti"
case blog = "Blog"
//BLOCCO 3 - SCOPRI SO2BUSINESS
case sitob = "Sito SO2 Business"
case entra = "Entra nel Team"
case blogb = "Business Blog"
//BLOCCO 4 - STRUMENTI BUSINESS
case bo = "Back Office"
case business = "Sponsor Link"
//BLOCCO 5 - CONTATTI
case webinar = "Webinar"
case circolari = "Circolari e Comunicazioni"
case domande = "Fai una Domanda!"
case seguici = "Seguici su"
}
class MenuController: UITableViewController {
public var delegate: MenuControllerDelegate?
private let menuItems: [SideMenuItem]
private let color = UIColor(red: 33 / 255.0, green: 33 / 255.0, blue: 33 / 255.0, alpha: 1)
init(with menuItems: [SideMenuItem]) {
self.menuItems = menuItems
super.init(nibName: nil, bundle: nil)
tableView.register(UITableViewCell.self,
forCellReuseIdentifier: "cell")
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
tableView.backgroundColor = UIColor.white
view.backgroundColor = UIColor.white
}
// MENU
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menuItems.count
}
//Recupero immagine da titolo
func getimage(titolo: String) -> UIImage {
var image: UIImage
switch titolo {
case "Scopri SynergyO2":
image = UIImage(named: "sito")!
case "Webinar":
image = UIImage(named: "webinar")!
case "Blog":
image = UIImage(named: "blog")!
case "Seguici su":
image = UIImage(named: "seguici")!
case "Fai una Domanda!":
image = UIImage(named: "domanda")!
case "Back Office":
image = UIImage(named: "backoffice")!
case "Circolari e Comunicazioni":
image = UIImage(named: "circolari")!
case "Area Business":
image = UIImage(named: "area")!
default:
image = UIImage(named: "prodotti")!
}
return image
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = menuItems[indexPath.row].rawValue
cell.textLabel?.textColor = .black
cell.textLabel?.font = UIFont.systemFont(ofSize: 13.0)
cell.backgroundColor = UIColor.white
cell.contentView.backgroundColor = UIColor.white
cell.imageView!.image = getimage(titolo: menuItems[indexPath.row].rawValue)
if menuItems[indexPath.row].rawValue == "Sito Web SynergyO2" {
cell.separatorInset = UIEdgeInsets.zero
cell.layoutMargins = UIEdgeInsets.zero
cell.preservesSuperviewLayoutMargins = false
cell.frame = CGRect(x: 0, y: 0, width: 1600, height: 200)
cell.contentView.backgroundColor = .white
let imageName = "menulogo.png"
let image = UIImage(named: imageName)
cell.imageView?.image = image
}
return cell
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if indexPath.row == 0 {
return 115
} else {
return 50
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
let selectedItem = menuItems[indexPath.row]
delegate?.didSelectMenuItem(named: selectedItem)
}
}
Have you tried to:
set self.automaticallyAdjustsScrollViewInsets = false on the table view controller? (i think it is deprecated and there is an alternaive call tho)
switch the table view to grouped/plain
Related
Have been trying to change the font size inside the UITableViewCell dynamically based on the device size.
Code to create a table inside a UIView (in an SKScene):
suitsView = UIView()
suitsView.backgroundColor = .purple
suitsView.alpha = 0
self.scene?.view?.addSubview(suitsView)
suitsTableView.register(suitsTableCell.self, forCellReuseIdentifier: "cellSuits")
suitsTableView.separatorColor = UIColor(red: 153/255, green: 255/255, blue: 153/255, alpha: 1)
suitsTableView.tableFooterView = UIView()
suitsTableView.allowsMultipleSelection = true
suitsTableView.reloadData()
suitsTableView.alpha = 0
suitsTableView.populateTable()
displayText() // Displays Text
suitsTableView.rowHeight = UITableViewAutomaticDimension
suitsTableView.estimatedRowHeight = 600
suitsView.addSubview(suitsTableView)
This is my UITableView:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let selectedIndexPaths = tableView.indexPathsForSelectedRows
let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath)
let cell : SuitsTableCell = tableView.dequeueReusableCell(withIdentifier: "cellSuits", for: indexPath as IndexPath) as! SuitsTableCell
cell.lblName.text = self.items[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return self.bounds.height/5
}
This is my UITableViewCell:
override init(style: UITableViewCellStyle, reuseIdentifier: String?)
{
super.init(style: style, reuseIdentifier: reuseIdentifier)
selectionStyle = UITableViewCellSelectionStyle.none
backgroundColor = UIColor.yellow
contentView.backgroundColor = UIColor.yellow
// Font size
var fontSize : CGFloat = 20.0
let marginGuide = contentView.layoutMarginsGuide
lblName = UILabel()
lblName.textColor = UIColor.black
lblName.font = UIFont(name:"Noteworthy-Bold", size: fontSize)
// CONSTRAINTS
lblName.translatesAutoresizingMaskIntoConstraints = false
lblName.topAnchor.constraint(equalTo: marginGuide.topAnchor).isActive = true
lblName.leadingAnchor.constraint(equalTo: marginGuide.leadingAnchor).isActive = true
lblName.bottomAnchor.constraint(equalTo: marginGuide.bottomAnchor).isActive = true
lblName.widthAnchor.constraint(equalToConstant: 200)
lblName.numberOfLines = 0
lblName.adjustsFontSizeToFitWidth = true
lblName.contentScaleFactor = 0.5
contentView.addSubview(lblName)
}
On iPhone 8, my table looks like this:
But on iPad, it looks like this:
I need to have a bigger font in iPad. How can I adjust the font size based on constraints or the row height?
Thanks.
Edit: I am not using Storyboard; please do not suggest it.
Interestingly, if I have this code inside my cellForRow method:
cell.lblName.font = UIFont(name:"Noteworthy-Bold", size: (tableView.bounds.height/14).rounded())
I can adjust the font size but the first row (None) is always empty...
You can use like this:
// Screen width.
public var screenWidth: CGFloat {
return UIScreen.main.bounds.width
}
// Screen height.
public var screenHeight: CGFloat {
return UIScreen.main.bounds.height
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let selectedIndexPaths = tableView.indexPathsForSelectedRows
let rowIsSelected = selectedIndexPaths != nil && selectedIndexPaths!.contains(indexPath)
let cell : SuitsTableCell = tableView.dequeueReusableCell(withIdentifier: "cellSuits", for: indexPath as IndexPath) as! SuitsTableCell
cell.lblName.text = self.items[indexPath.row]
cell.lblName.font = UIFont(name:"Noteworthy-Bold", size: (screenwidth * 0.0373).rounded()) // 0.0373 = requiredfontsize / 375
return cell
}
Note: I don't know whether this method is right or wrong but I have tried this and it worked for me.
You can go through this for more info: Scale text label by screen size
Use Size-Class and hit on + symbol just before the Font tab (in storyboard) and choose Regular * Regular and set the font size as per your wish.
Using Swift 3 with Xcode 9 I'm using the large UINavigationBar view by:
if #available(iOS 11.0, *) {
navigationBar?.prefersLargeTitles = true
UINavigationBar.appearance().largeTitleTextAttributes = [
NSForegroundColorAttributeName: Colors.black
]
}
When I scroll a UITableView, the bar collapses too fast which creates an unwanted space:
Before:
After:
The moment I touch the UITableView the bar collapses.
The tableView has the following properties:
let rect = CGRect(
x: 0,
y: UIApplication.shared.statusBarView?.frame.height ?? 20,
width: UIScreen.main.bounds.width,
height: UIScreen.main.bounds.height
)
let tableView = UITableView(frame: rect)
The top inset of the tableView is self.navigationController?.navigationBar.frame.height ?? 44
Also the tableView is set to:
if #available(iOS 11.0, *) {
self.contentInsetAdjustmentBehavior = .never
}
The bar is translucent and I wish to keep that. What am I missing? Help is very appreciated.
I don't know whether it will useful to you or not. But its the sample code which is working for me.
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchResultsUpdating {
var numbers: [String] = []
let rect = CGRect(
x: 0,
y: UIApplication.shared.statusBarFrame.height ?? 20,
width: UIScreen.main.bounds.width,
height: UIScreen.main.bounds.height
)
lazy var tableView: UITableView = {
let tV = UITableView()
tV.delegate = self
tV.dataSource = self
tV.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "cell")
return tV
}()
override func viewDidLoad() {
super.viewDidLoad()
numbers = generateNumbers()
self.view.backgroundColor = UIColor.white
title = "Numbers"
self.navigationController?.navigationBar.prefersLargeTitles = true
let search = UISearchController(searchResultsController: nil)
search.hidesNavigationBarDuringPresentation = false
search.searchResultsUpdater = self
search.definesPresentationContext = true
self.navigationItem.searchController = search
tableView.frame = rect
self.view.addSubview(tableView)
}
func generateNumbers() -> [String] {
var numbers = [String]()
for var i in 1..<100 {
numbers.append(String(i))
}
return numbers
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return numbers.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = self.numbers[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
}
func updateSearchResults(for searchController: UISearchController) {
if let text = searchController.searchBar.text, !text.isEmpty {
numbers = self.numbers.filter({ (number) -> Bool in
return number.contains(text)
})
} else {
numbers = generateNumbers()
}
self.table.reloadData()
}
}
I got it working using:
if #available(iOS 11.0, *) {
self.contentInsetAdjustmentBehavior = .always
}
And
let tableViewFrame = CGRect(
x: 0,
y: UIApplication.shared.statusBarFrame.height ?? 20,
width: UIScreen.main.bounds.width,
height: UIScreen.main.bounds.height
)
And no further TableView top insets.
I am Using UITableView For Display some data. But I have Seperated the Datasource and Delegate Methods to another class. So my TableViewController is not having Any delegate Methods. They are in Seperate Class.
So when the tableview loads for the First Time, It loads the values. but when i use tableView.reloadData on a button click of a cell , It does not calls the cellForRawAtIndexPath Method. But it calls numberOfRawsInSection and other methods.
Here is my Controller Class
class AdvancedSearch: UITableViewController , PopOverViewDelegate{
#IBOutlet var model: AdvancedSearchDataModel!
let dataModel = AdvancedSearchDataModel()
override func viewDidLoad() {
super.viewDidLoad()
tableView.registerNib(UINib(nibName: "AgeCell", bundle: nil), forCellReuseIdentifier: "AgeCell")
tableView.registerNib(UINib(nibName: "ExperienceCell", bundle: nil), forCellReuseIdentifier: "ExperienceCell")
tableView.registerNib(UINib(nibName: "CityCell", bundle: nil), forCellReuseIdentifier: "CityCell")
tableView.registerNib(UINib(nibName: "StateCell", bundle: nil), forCellReuseIdentifier: "StateCell")
tableView.registerNib(UINib(nibName: "DateTimeCell", bundle: nil), forCellReuseIdentifier: "DateTimeCell")
tableView.registerNib(UINib(nibName: "GenderCell", bundle: nil), forCellReuseIdentifier: "GenderCell")
tableView.registerNib(UINib(nibName: "ResultButtonCell", bundle: nil), forCellReuseIdentifier: "ResultButtonCell")
//tableView.dataSource = model
//tableView.delegate = model
tableView.separatorColor = UIColor.grayColor()
tableView.allowsSelection = false
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
tableView.reloadData()
}
// MARK: - DropDown PopOver Delegates
func valueOfTheFieldWhenSelect(tableViewCell: UITableViewCell, value: String, indexPath: NSIndexPath) {
let data = AdvancedSearchDataModel()
data.buttonTitle = value
self.tableView.delegate = data
self.tableView.dataSource = data
self.tableView.reloadData()
}
Here is my Datasource and Delegates in Seperate Class
class AdvancedSearchDataModel: NSObject , UITableViewDataSource , UITableViewDelegate , AdvanceSearchDropDownButtonDelegate , UIPopoverPresentationControllerDelegate {
let data = ["Age","Experience","City" , "State" , "Gender" , "Date and Time"]
// set this proprty in PopOverViewDelegate method
var buttonTitle : String?
// MARK: - TableView Datasource
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("Count : \(data.count)")
return data.count + 1
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
switch indexPath.row {
case 0:
let cell = tableView.dequeueReusableCellWithIdentifier("AgeCell", forIndexPath: indexPath) as! AgeCell
applyStyleForButton(cell.selectAge)
let frame = CGRectMake(0, cell.frame.size.height - 2, cell.frame.size.width,2)
let additionalSeparator = UIView(frame: frame)
additionalSeparator.backgroundColor = UIColor.grayColor()
cell.addSubview(additionalSeparator)
if buttonTitle != nil {
cell.selectAge.setTitle(buttonTitle, forState: .Normal)
}
cell.indexPath = indexPath
cell.dropDownDelegate = self
return cell
case 1:
let cell = tableView.dequeueReusableCellWithIdentifier("ExperienceCell", forIndexPath: indexPath) as! ExperienceCell
applyStyleForButton(cell.selectExperinceBtn)
let frame = CGRectMake(0, cell.frame.size.height - 2, cell.frame.size.width,2)
let additionalSeparator = UIView(frame: frame)
additionalSeparator.backgroundColor = UIColor.grayColor()
cell.addSubview(additionalSeparator)
return cell
case 2:
let cell = tableView.dequeueReusableCellWithIdentifier("CityCell", forIndexPath: indexPath) as! CityCell
applyStyleForButton(cell.cityButton)
let frame = CGRectMake(0, cell.frame.size.height - 2, cell.frame.size.width,2)
let additionalSeparator = UIView(frame: frame)
additionalSeparator.backgroundColor = UIColor.grayColor()
cell.addSubview(additionalSeparator)
return cell
case 3:
let cell = tableView.dequeueReusableCellWithIdentifier("StateCell", forIndexPath: indexPath) as! StateCell
applyStyleForButton(cell.stateButton)
let frame = CGRectMake(0, cell.frame.size.height - 2, cell.frame.size.width,2)
let additionalSeparator = UIView(frame: frame)
additionalSeparator.backgroundColor = UIColor.grayColor()
cell.addSubview(additionalSeparator)
return cell
case 4:
let cell = tableView.dequeueReusableCellWithIdentifier("GenderCell", forIndexPath: indexPath) as! GenderCell
let frame = CGRectMake(0, cell.frame.size.height - 2, cell.frame.size.width,2)
let additionalSeparator = UIView(frame: frame)
additionalSeparator.backgroundColor = UIColor.grayColor()
cell.addSubview(additionalSeparator)
return cell
case 5:
let cell = tableView.dequeueReusableCellWithIdentifier("DateTimeCell", forIndexPath: indexPath) as! DateTimeCell
applyStyleForButton(cell.datebutton)
applyStyleForButton(cell.timeButton)
let frame = CGRectMake(0, cell.frame.size.height - 2, cell.frame.size.width,2)
let additionalSeparator = UIView(frame: frame)
additionalSeparator.backgroundColor = UIColor.grayColor()
cell.addSubview(additionalSeparator)
return cell
case 6:
let cell = tableView.dequeueReusableCellWithIdentifier("ResultButtonCell", forIndexPath: indexPath) as! ResultButtonCell
let frame = CGRectMake(0, cell.frame.size.height - 2, cell.frame.size.width,2)
let additionalSeparator = UIView(frame: frame)
additionalSeparator.backgroundColor = UIColor.grayColor()
cell.addSubview(additionalSeparator)
return cell
default:
return UITableViewCell()
}
}
// MARK: - TableView Delegates
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 5 {
return 150
}else{
return 94
}
}
// MARK: - Styling
func applyStyleForButton(button : UIButton){
button.layer.borderColor = UIColor.grayColor().CGColor
button.layer.borderWidth = 1
button.layer.cornerRadius = 5
button.backgroundColor = UIColor.clearColor()
}
// MARK: - DropDown Delegates
func openAgeDropDown(by button: UIButton, and cell: AgeCell,indexPath : NSIndexPath)
{
print("Age opened")
let tableViewController = DropDownController(style: .Plain, menuItems: ["10","20","30"],cell: cell , index: indexPath)
tableViewController.modalPresentationStyle = UIModalPresentationStyle.Popover
tableViewController.preferredContentSize = CGSizeMake(button.frame.width, button.frame.width)
// assigning the delegate in tableView to fill the textfield
tableViewController.delegate = AdvancedSearch()
let popoverPresentationController = tableViewController.popoverPresentationController
popoverPresentationController?.permittedArrowDirections = .Any
popoverPresentationController?.delegate = self
popoverPresentationController?.sourceView = button
popoverPresentationController?.sourceRect = button.bounds
UIApplication.sharedApplication().keyWindow?.rootViewController!.presentViewController(tableViewController, animated: true, completion: nil)
}
// MARK: - PopOver Delegates
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None
}}
Please Guide me
The methods is not calling because everytime you are creating a new object of AdvancedSearchDataModel. instead this assign delegate in viewDidLoad. don't need to create new object of AdvancedSearchDataModel
in viewDidLoad
...
tableView.separatorColor = UIColor.grayColor()
tableView.allowsSelection = false
tableView.separatorStyle = UITableViewCellSeparatorStyle.None
tableView.delegate = self.model
tableView.dataSource = self.model
and in valueOfTheFieldWhenSelect
func valueOfTheFieldWhenSelect(tableViewCell: UITableViewCell, value: String, indexPath: NSIndexPath) {
self.model.buttonTitle = value
self.tableView.reloadData()
}
UITableViewCell Custom UIImageView
Image:
Custom TableViewCell UIImageView
If you click on the link above, you'll see that after the first row, the custom UIImageView is being cut off horizontally and then vertically in subsequent rows. Any ideas on how to fix this?
In viewDidLoad I changes row heights as follows:
// Set cell row height
self.tableView.rowHeight = 60
Here's the code for the TableView:
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
var count = Int()
if self.names.count != 0 {
count = self.names.count
} else if self.names.count == 0 {
count = 1
}
return count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell : UITableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
for view in cell.contentView.subviews {
// Clear subviews
view.removeFromSuperview()
}
// Customize separator width
tableView.separatorInset = UIEdgeInsetsZero
cell.separatorInset = UIEdgeInsetsZero
cell.layoutMargins = UIEdgeInsetsZero
cell.preservesSuperviewLayoutMargins = false
// Create imageView
let imageView = UIImageView(frame: CGRectMake(10, 5, 50, 50))
imageView.contentMode = UIViewContentMode.ScaleAspectFit
imageView.layer.cornerRadius = 25
imageView.clipsToBounds = true
cell.contentView.addSubview(imageView)
// Create textLabel
let textLabel = UILabel(frame: CGRectMake(70, 10, self.view.frame.width, 20))
textLabel.font = UIFont(name: "Arial", size: 15.0)
textLabel.textAlignment = .Left
cell.contentView.addSubview(textLabel)
// Create lastMessageLabel
let lastMessageLabel = UILabel(frame: CGRectMake(70, 30, self.view.frame.width, 20))
lastMessageLabel.font = UIFont(name: "Arial", size: 12)
lastMessageLabel.textAlignment = .Left
lastMessageLabel.textColor = UIColor.grayColor()
cell.contentView.addSubview(lastMessageLabel)
if self.names.count != 0 {
// Add contact cell
imageView.image = self.images[indexPath.row]
textLabel.text = self.names[indexPath.row] as? String
lastMessageLabel.text = self.lastMessage[indexPath.row] as? String
}
if self.names.count == 0 {
// Add warning
imageView.image = UIImage(named: "user-placeholder.png")
textLabel.text = "No users saved :("
lastMessageLabel.text = "Tap the search tab to find users."
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if self.names.count != 0 {
print(self.names.count)
print(self.names[indexPath.row])
print(self.ids[indexPath.row])
self.selectedUserName = self.names[indexPath.row] as! String
self.selectedUserId = self.ids[indexPath.row] as! String
self.selectedUserImage = self.images[indexPath.row]
self.performSegueWithIdentifier("chat", sender: self)
}
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == UITableViewCellEditingStyle.Delete && self.userRef.savedUsers.count != 0 {
startActivityIndicator()
let savedUsers = NSMutableArray()
for user in self.userRef.savedUsers {
if user as! String != self.userRef.savedUsers[indexPath.row] as! String {
savedUsers.addObject(user as! String)
}
}
self.rootRef.child("users").child((FIRAuth.auth()?.currentUser?.uid)!).child("savedUsers").setValue(savedUsers, withCompletionBlock: { (error: NSError?, FIRDatabaseReference: FIRDatabaseReference) -> Void in
if error == nil {
print("deleted: \(self.userRef.savedUsers[indexPath.row])")
// Remove user
self.userRef.savedUsers.removeObject(self.userRef.savedUsers[indexPath.row])
self.names.removeObjectAtIndex(indexPath.row)
self.images.removeAtIndex(indexPath.row)
self.lastMessage.removeObjectAtIndex(indexPath.row)
self.stopActivityIndicator()
self.loadUsers()
self.displayAlert("Contact Deleted", message: "The user has been removed from your contacts.")
} else {
self.stopActivityIndicator()
self.displayAlert("Delete Failed", message: "Please try again later.")
}
})
}
}
EDIT:
The problem was imageView.contentMode = UIViewContentMode.ScaleAspectFit which needed to be .ScaleAspectFill
I have create a custom delete function that i called toDoItemDeleted but there seem to be an error
.
.
Any idea how to solve it?( i m following this tutorial)
The tableview
class TodolistTable: UITableViewController , TableViewCellDelegate {
var todoItems: [DataSource] = []
override func viewDidLoad() {
self.tableView.reloadData()
tableView.backgroundView = UIImageView(image: UIImage(named: "backgroundTableView"))
tableView.dataSource = self
tableView.delegate = self
tableView.registerClass(TableViewCell.self, forCellReuseIdentifier: "Cell")
tableView.separatorStyle = .None
tableView.backgroundColor = UIColor.blackColor()
tableView.rowHeight = 50;
}
override func viewWillAppear(animated: Bool) {
self.tableView.reloadData()
}
#IBAction func unwinTodlist (segue: UIStoryboardSegue){
let source = segue.sourceViewController as! ViewController
let todoItemx : DataSource = source.todoitemer
if todoItemx.itenName != "" {
self.todoItems.append(todoItemx)
self.tableView.reloadData()
}
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return todoItems.count }
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let TempCell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
as! TableViewCell
let todoItem = todoItems[indexPath.row]
//TempCell.textLabel?.backgroundColor = UIColor.clearColor()
TempCell.selectionStyle = .None
TempCell.textLabel?.text = todoItem.itenName
TempCell.delegate = self
TempCell.Item = todoItem
return TempCell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: false)
let tappedItem = todoItems [indexPath.row] as DataSource
tappedItem.completed = !tappedItem.completed
tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation:UITableViewRowAnimation.None)
}
override func tableView(tableView: UITableView, heightForRowAtIndexPath
indexPath: NSIndexPath) -> CGFloat {
return tableView.rowHeight;
}
func toDoItemDeleted(Item2: DataSource) {
// could use this to get index when Swift Array indexOfObject works
// let index = toDoItems.indexOfObject(toDoItem)
// in the meantime, scan the array to find index of item to delete
let index = (todoItems as NSArray).indexOfObject(Item2)
if index == NSNotFound{return}
// could removeAtIndex in the loop but keep it here for when indexOfObject works
todoItems.removeAtIndex(index)
// use the UITableView to animate the removal of this row
tableView.beginUpdates()
let indexPathForRow = NSIndexPath(forRow: index, inSection: 0)
tableView.deleteRowsAtIndexPaths([indexPathForRow], withRowAnimation: .Fade)
tableView.endUpdates()
}
// MARK: - Table view delegate
func colorForIndex(index: Int) -> UIColor {
let itemCount = todoItems.count - 1
let val = (CGFloat(index) / CGFloat(itemCount)) * 0.6
return UIColor(red: 1.0, green: val, blue: 0.0, alpha: 1.0)
}
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell,
forRowAtIndexPath indexPath: NSIndexPath) {
cell.backgroundColor = colorForIndex(indexPath.row)
}
}
The tableviewCell
import UIKit
import QuartzCore
// A protocol that the TableViewCell uses to inform its delegate of state change
protocol TableViewCellDelegate {
// indicates that the given item has been deleted
func toDoItemDeleted(todoItem: DataSource)
}
class TableViewCell: UITableViewCell {
let gradientLayer = CAGradientLayer()
var originalCenter = CGPoint()
var deleteOnDragRelease = false, completeOnDragRelease = false
var tickLabel: UILabel, crossLabel: UILabel
let label: StrikeThroughText
var itemCompleteLayer = CALayer()
// The object that acts as delegate for this cell.
var delegate: TableViewCellDelegate?
// The item that this cell renders.
var Item: DataSource? {
didSet {
//label.text = Item!.text
label.strikeThrough = Item!.completed
itemCompleteLayer.hidden = !label.strikeThrough
}
}
required init(coder aDecoder: NSCoder) {
fatalError("NSCoding not supported")
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
// create a label that renders the to-do item text
label = StrikeThroughText(frame: CGRect.null)
label.textColor = UIColor.whiteColor()
label.font = UIFont.boldSystemFontOfSize(16)
label.backgroundColor = UIColor.clearColor()
// utility method for creating the contextual cues
func createCueLabel() -> UILabel {
let label = UILabel(frame: CGRect.null)
label.textColor = UIColor.whiteColor()
label.font = UIFont.boldSystemFontOfSize(32.0)
label.backgroundColor = UIColor.clearColor()
return label
}
// tick and cross labels for context cues
tickLabel = createCueLabel()
tickLabel.text = "\u{2713}"
tickLabel.textAlignment = .Right
crossLabel = createCueLabel()
crossLabel.text = "\u{2717}"
crossLabel.textAlignment = .Left
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(label)
addSubview(tickLabel)
addSubview(crossLabel)
// remove the default blue highlight for selected cells
selectionStyle = .None
// gradient layer for cell
gradientLayer.frame = bounds
let color1 = UIColor(white: 1.0, alpha: 0.2).CGColor as CGColorRef
let color2 = UIColor(white: 1.0, alpha: 0.1).CGColor as CGColorRef
let color3 = UIColor.clearColor().CGColor as CGColorRef
let color4 = UIColor(white: 0.0, alpha: 0.1).CGColor as CGColorRef
gradientLayer.colors = [color1, color2, color3, color4]
gradientLayer.locations = [0.0, 0.01, 0.95, 1.0]
layer.insertSublayer(gradientLayer, atIndex: 0)
// add a layer that renders a green background when an item is complete
itemCompleteLayer = CALayer(layer: layer)
itemCompleteLayer.backgroundColor = UIColor(red: 0.0, green: 0.6, blue: 0.0, alpha: 1.0).CGColor
itemCompleteLayer.hidden = true
layer.insertSublayer(itemCompleteLayer, atIndex: 0)
// add a pan recognizer
let recognizer = UIPanGestureRecognizer(target: self, action: #selector(TableViewCell.handlePan(_:)))
recognizer.delegate = self
addGestureRecognizer(recognizer)
}
let kLabelLeftMargin: CGFloat = 15.0
let kUICuesMargin: CGFloat = 10.0, kUICuesWidth: CGFloat = 50.0
override func layoutSubviews() {
super.layoutSubviews()
// ensure the gradient layer occupies the full bounds
gradientLayer.frame = bounds
itemCompleteLayer.frame = bounds
label.frame = CGRect(x: kLabelLeftMargin, y: 0,
width: bounds.size.width - kLabelLeftMargin, height: bounds.size.height)
tickLabel.frame = CGRect(x: -kUICuesWidth - kUICuesMargin, y: 0,
width: kUICuesWidth, height: bounds.size.height)
crossLabel.frame = CGRect(x: bounds.size.width + kUICuesMargin, y: 0,
width: kUICuesWidth, height: bounds.size.height)
}
//MARK: - horizontal pan gesture methods
func handlePan(recognizer: UIPanGestureRecognizer) {
// 1
if recognizer.state == .Began {
// when the gesture begins, record the current center location
originalCenter = center
}
// 2
if recognizer.state == .Changed {
let translation = recognizer.translationInView(self)
center = CGPointMake(originalCenter.x + translation.x, originalCenter.y)
// has the user dragged the item far enough to initiate a delete/complete?
deleteOnDragRelease = frame.origin.x < -frame.size.width / 2.0
completeOnDragRelease = frame.origin.x > frame.size.width / 2.0
// fade the contextual clues
let cueAlpha = fabs(frame.origin.x) / (frame.size.width / 2.0)
tickLabel.alpha = cueAlpha
crossLabel.alpha = cueAlpha
// indicate when the user has pulled the item far enough to invoke the given action
tickLabel.textColor = completeOnDragRelease ? UIColor.greenColor() : UIColor.whiteColor()
crossLabel.textColor = deleteOnDragRelease ? UIColor.redColor() : UIColor.whiteColor()
}
// 3
if recognizer.state == .Ended {
let originalFrame = CGRect(x: 0, y: frame.origin.y,
width: bounds.size.width, height: bounds.size.height)
if deleteOnDragRelease {
if delegate != nil && Item != nil {
// notify the delegate that this item should be deleted
delegate!.toDoItemDeleted(Item!)
}
} else if completeOnDragRelease {
if Item != nil {
Item!.completed = true
}
label.strikeThrough = true
itemCompleteLayer.hidden = false
UIView.animateWithDuration(0.2, animations: {self.frame = originalFrame})
} else {
UIView.animateWithDuration(0.2, animations: {self.frame = originalFrame})
}
}
}
override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
if let panGestureRecognizer = gestureRecognizer as? UIPanGestureRecognizer {
let translation = panGestureRecognizer.translationInView(superview!)
if fabs(translation.x) > fabs(translation.y) {
return true
}
return false
}
return false
}
}
Don't use a UIGestureRecognizer. You can create delete actions easily:
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
return true
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
// delete object
}
}