Custom TableView Cell Missing in IOS Xcode 7 Using Swift 2 - uitableview

In Xcode 7 I am developing custom tableView so their is a problem each 2 cells are unprinted after one. I do not know how this problem is coming from?
Here is my custom cell class:
import UIKit
class CustomCellTwo: UITableViewCell {
#IBOutlet weak var indx: UILabel!
#IBOutlet weak var dateOne: UILabel!
#IBOutlet weak var duaration: UILabel!
#IBOutlet weak var distance: UILabel!
#IBOutlet weak var ignitionOff: UILabel!
#IBOutlet weak var locationOne: UILabel!
#IBOutlet weak var dateTwo: UILabel!
#IBOutlet weak var ignitionOn: UILabel!
#IBOutlet weak var locationTwo: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
And here is my ViewControllerClass :
import Foundation
import UIKit
class TripReport : UIViewController, UITableViewDelegate, UITableViewDataSource{
var dateTimeOn = [ "2016-Jan-20 11:36 AM","2016-Jan-20 11:25 AM","2016-Jan-20 11:15 AM","2016-Jan-20 11:05 AM","2016-Jan-20 11:04 AM","2016-Jan-20 11:02 AM","2016-Jan-20 11:00 AM","2016-Jan-20 10:57 AM","2016-Jan-20 10:57 AM","2016-Jan-20 10:53 AM"]
var dateTimeOff = [ "2016-Jan-20 11:36 AM","2016-Jan-20 11:25 AM","2016-Jan-20 11:15 AM","2016-Jan-20 11:05 AM","2016-Jan-20 11:04 AM","2016-Jan-20 11:02 AM","2016-Jan-20 11:00 AM","2016-Jan-20 10:57 AM","2016-Jan-20 10:57 AM","2016-Jan-20 10:53 AM"]
var eventOn = [ "IGNITION","IGNITION ON","IGNITION ON","IGNITION ON","IGNITION ON","IGNITION ON","IGNITION ON","IGNITION ON","IGNITION ON","IGNITION ON"]
var eventOff = [ "IGNITION OFF","IGNITION OFF","IGNITION OFF","IGNITION OFF","IGNITION OFF","IGNITION OFF","IGNITION OFF","IGNITION OFF","IGNITION OFF","IGNITION OFF"]
var duaration = [ "00 hr 00 min","00 hr 00 min","00 hr 00 min","00 hr 00 min","00 hr 00 min","00 hr 00 min","00 hr 00 min","00 hr 00 min","00 hr 00 min","00 hr 00 min"]
var distance = [ "00 km","00 km","00 km","00 km","00 km","00 km","00 km","00 km","00 km","00 km"]
var locationOn = [ "7.88 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK","0.47 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK","0.99 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK","0.99 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK","0.99 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK","1.01 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK","1.16 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK","1.03 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK","0.98 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK","1.62 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK"]
var locationOff = [ "7.88 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK","0.47 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK","0.99 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK","0.99 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK","0.99 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK","1.01 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK","1.16 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK","1.03 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK","0.98 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK","1.62 KM SE of Police Line Constibilary, Faisalabad - Jaranwala Road, FAISALABAD, PUNJAB, PK"]
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
print("Trip Report Called")
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10;
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("celltwo", forIndexPath: indexPath) as! CustomCellTwo
print("table view cell")
cell.dateOne.text = self.dateTimeOff[indexPath.row]
cell.duaration.text = self.duaration[indexPath.row]
cell.distance.text = self.distance[indexPath.row]
cell.ignitionOn.text = self.eventOn[indexPath.row]
cell.ignitionOff.text = self.eventOff[indexPath.row]
cell.locationOne.text = self.locationOff[indexPath.row]
cell.locationTwo.text = self.locationOn[indexPath.row]
cell.dateOne.text = self.dateTimeOff[indexPath.row]
cell.dateTwo.text = self.dateTimeOn[indexPath.row]
cell.indx.text = "\(indexPath.row)";
return cell
}
}
Note : I have the same technique for other tableview and that is working perfect .
Now my both Views are joined in Tab Bar Controller ..

Related

I am getting "Error" countdowns in my EggTimer code. It's supposed to be counting down the time each egg takes

class ViewController: UIViewController {
let eggTimes = ["Soft": 5, "Medium": 7, "Hard" : 12]
#IBOutlet weak var eggCountdown2: UILabel!
#IBOutlet weak var eggCountDown: UILabel!
#IBOutlet weak var eggCountdown3: UILabel!
var count1 = 7
var count2 = 5
var count3 = 12
override func viewDidLoad() {
super.viewDidLoad()
var timer = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(UIMenuController.update), userInfo: nil, repeats: true)
}
#objc func update() {
switch UILabel() {
case eggCountDown:
print("\(count1) seconds to get your medium egg")
count1 -= 1
case eggCountdown2 :
print("\(count2) seconds to get your soft egg")
count2 -= 1
case eggCountdown3 :
print("\(count3) seconds to get your fucking hard egg")
count3 -= 1
default:
print ("Error")
}
}
#IBAction func hardnessSelected(_ sender: UIButton) {
let hardness = sender.currentTitle!
print(eggTimes [hardness]!)
}
}
I am getting "Error" countdowns in my EggTimer code. It's supposed to be counting down the time each egg takes. I have tried multiple ways to tackle this but always ended with up this. Also I have been scraping Apple documentation for the same but still cannot find the relevant answer.
Screen shot:
Rather than switching on an unrelated label – which cannot work – create a property for the hardness
var hardness = ""
and one counter is sufficient as you apparently not going to run two timers simultaneously.
var counter = 0
and assign both values in hardnessSelected
#IBAction func hardnessSelected(_ sender: UIButton) {
self.hardness = sender.currentTitle!
self.counter = eggtimes[hardness]!
print(counter)
}
Then switch this way
#objc func update() {
switch hardness {
case "Medium":
print("\(count1) seconds to get your medium egg")
counter -= 1
case "Soft" :
print("\(count2) seconds to get your soft egg")
counter -= 1
case "Hard" :
print("\(count3) seconds to get your fucking hard egg")
counter -= 1
default:
print ("Error")
}
}
And you need to check when the countdown ends and to start the timer in the IBAction. And to avoid bad cooking experience set the repeat interval to 60 (one minute) or multiply the egg times by 300.

count-down timer not updating .text

Text to count These are the numbers I like to count down, S= start, O= stop, R= reset. Added for simpler functionality but do not want to use them.
I'm trying to set a countdown timer on this app, but I'm having issues getting it to work with the tutorials I've seen. My guess is that this version of swift has different functions, Xcode version 9.4.
The goal is to have a counter for a year, days, and time, as each runs out it stops and counts the next, preferably I want random numbers, no start or stop buttons, start can be once app opens or if the user accepts on a previous screen.
I have tried these methods, but they either don't do anything or force me to use #object func counter(), I believe the timer is not calling the function once its an object, I don't know how to call it.
import UIKit
class ViewController: UIViewController {
var seconds = 30
var timer = Timer()
//these are others I will implement later
#IBOutlet weak var YRS: UILabel!
var yrs = 1
#IBOutlet weak var DAY: UILabel!
var day = 3
#IBOutlet weak var HRS: UILabel!
var hrs = 5
#IBOutlet weak var SEC: UILabel!
var sec = 35
#IBOutlet weak var MIN: UILabel!
//timer function, I believe I have to change #selector as is not
calling the counter func
#IBAction func str(_ sender: UIButton) {
//I have also used just counter but nothing changes :/
timer = Timer.init(timeInterval: 60.0, target: self, selector:
#selector(ViewController.counter), userInfo: nil, repeats: true)
MIN.text = String (seconds)
}
//stop button
#IBAction func sto(_ sender: Any) {
}
//reset button
#IBAction func rst(_ sender: Any) {
}
//counter here -1
#objc func counter(){
seconds -= 1
MIN.text = String(seconds)
if (seconds == 00) {
timer.invalidate()
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
//Something I have tried but did not work
_ = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(UIMenuController.update), userInfo: nil, repeats: true)
}
func update() {
if(min > 0) {
MIN.text = String(min-1)
}
}
//No errors found, the screen is updated with the value but it does not change.
This was a test in Playground. But you can of course also use it in your viewDidLoad Method. It is not ready, you just have to add code where i commented it (add code here...). I set the timer to 0.1 seconds, so that i could test it.
var YRS = UILabel()
var yrs = 1
var DAY = UILabel()
var day = 3
var HRS = UILabel()
var hrs = 5
var SEC = UILabel()
var sec = 35
var MIN = UILabel()
var min = 15
YRS.text = "years \(yrs)"
YRS.sizeToFit()
view.addSubview(YRS)
DAY.text = "day \(day)"
DAY.sizeToFit()
DAY.frame.origin.y = 30
view.addSubview(DAY)
HRS.text = "HRS \(hrs)"
HRS.sizeToFit()
HRS.frame.origin.y = 60
view.addSubview(HRS)
MIN.text = "min \(day)"
MIN.sizeToFit()
MIN.frame.origin.y = 90
view.addSubview(MIN)
SEC.text = "SEC \(sec)"
SEC.sizeToFit()
SEC.frame.origin.y = 120
view.addSubview(SEC)
Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { (timer) in
sec = sec - 1
if sec < 0 { sec = 59
min = min - 1
if min < 0 { min = 59
hrs = hrs - 1
if hrs < 0 {
hrs = 23
HRS.text = "HRS \(hrs)"
HRS.sizeToFit()
// add code here for day ...
}
}
MIN.text = "min \(min)"
MIN.sizeToFit()
}
SEC.text = "sec \(sec)"
SEC.sizeToFit()
}

Bug in Xcode when the text is put into a Label

Here's my situation:
There are 8 ImageViews. The 4 on the left are called in my code (starting at the top): Left1, Left2, Left3, left4. The 4 on the right (always starting at the top): Right1, Right2, Right3, Right4.
Then there are two red Buttons in the middle: "Up" and "Down" and a Label.
What should happen is that when the Button Up is pressed, all the ImageViews go to a position which is above the top of the screen (so their center.y < 0) that I've set in the code, and when this happens the label text is set to "Up".
When on the contrary I press the Button Down, all the ImageViews go back to the initial position and the label text is set to "Down".
What happens instead is that the label.text changes according to my code, but my ImageViews don't move.
Here's my code:
First i declare the objects:
class ViewController: UIViewController {
#IBOutlet weak var Up: UIButton!
#IBOutlet weak var Down: UIButton!
#IBOutlet weak var Lab: UILabel!
#IBOutlet weak var Left1: UIImageView!
#IBOutlet weak var Left2: UIImageView!
#IBOutlet weak var Left3: UIImageView!
#IBOutlet weak var Left4: UIImageView!
#IBOutlet weak var Right1: UIImageView!
#IBOutlet weak var Right2: UIImageView!
#IBOutlet weak var Right3: UIImageView!
#IBOutlet weak var Right4: UIImageView!
In the View Did Load I hide all the ImageViews:
Left1.hidden = true
Left2.hidden = true
Left3.hidden = true
Left4.hidden = true
Right1.hidden = true
Right2.hidden = true
Right3.hidden = true
Right4.hidden = true
Then there are the two Buttons actions:
-Button Up
#IBAction func Up(sender: AnyObject) {
Lab.text = "Up"
//-------- 1 ----------//
Right1.center.y = 0 - 31
Left1.center.y = 0 - 31
Right1.hidden = false
Left1.hidden = false
//-------- 2 ----------//
Right2.center.y = 0 - 300
Left2.center.y = 0 - 300
Right2.hidden = false
Left2.hidden = false
//-------- 3 ----------//
Right3.center.y = 0 - 650
Left3.center.y = 0 - 650
Right3.hidden = false
Left3.hidden = false
//-------- 4 ----------//
Right4.center.y = 0 - 960
Left4.center.y = 0 - 960
Right4.hidden = false
Left4.hidden = false
}
Here I put the Label text to "Up" and, for every couple of ImageView (when I say couple I mean that the ImageViews at the top of the image (1) are the Couple 1, the ones at the bottom the Couple 4) I set their center.y to a value above the screen and then i show them.
-Button Down
#IBAction func Down(sender: AnyObject) {
Lab.text = "Down"
//-------- 1 ----------//
Right1.center.y = 92
Left1.center.y = 92
//-------- 2 ----------//
Right2.center.y = 171
Left2.center.y = 171
//-------- 3 ----------//
Right3.center.y = 249
Left3.center.y = 249
//-------- 4 ----------//
Right4.center.y = 326
Left4.center.y = 326
}
Here i just put the label text to "down" and the ImageViews back to the initial positions.
Now, this should work perfectly, but what happens is that the label text is changed to up when I press the Up Button, but the ImageViews don't move, just appear.
On the contrary if I comment the label instructions then it works:
-Button Up
#IBAction func Up(sender: AnyObject) {
//Lab.text = "Up"
//-------- 1 ----------//
Right1.center.y = 0 - 31
Left1.center.y = 0 - 31
Right1.hidden = false
Left1.hidden = false
//-------- 2 ----------//
Right2.center.y = 0 - 300
Left2.center.y = 0 - 300
Right2.hidden = false
Left2.hidden = false
//-------- 3 ----------//
Right3.center.y = 0 - 650
Left3.center.y = 0 - 650
Right3.hidden = false
Left3.hidden = false
//-------- 4 ----------//
Right4.center.y = 0 - 960
Left4.center.y = 0 - 960
Right4.hidden = false
Left4.hidden = false
}
-Button Down
#IBAction func Down(sender: AnyObject) {
//Lab.text = "Down"
//-------- 1 ----------//
Right1.center.y = 92
Left1.center.y = 92
//-------- 2 ----------//
Right2.center.y = 171
Left2.center.y = 171
//-------- 3 ----------//
Right3.center.y = 249
Left3.center.y = 249
//-------- 4 ----------//
Right4.center.y = 326
Left4.center.y = 326
}
In this way the ImageViews move as they should, but of course the label text doesn't change.
Is there any relationship between the label and the imageviews?
I really hope you can help me, this is crazy.
Thank you

How to populate UITextField with UIPickerView that have 3 components?

I did a date customized UIPickerView in my ViewController that have 3 components.
This picker work as an input to a UITextField. See some parts of my code to check how it was implemented.
var dataDia1: NSMutableArray!
var dataMes1: NSMutableArray!
var dataAno1: NSMutableArray!
#IBOutlet weak var pickerTextFieldInicio: UITextField!
let pickerViewInicio = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
// Declaração dos Picker
pickerViewInicio.delegate = self
pickerViewInicio.dataSource = self
pickerViewInicio.showsSelectionIndicator = true
pickerViewInicio.backgroundColor = UIColor(red: 255/255, green: 193/255, blue: 193/255, alpha: 1)
self.dataDia1 = NSMutableArray()
for dia in 1...31
{
dataDia1.addObject("\(dia)")
}
self.dataMes1 = NSMutableArray()
for mes in 1...12
{
dataMes1.addObject("\(mes)")
}
self.dataAno1 = NSMutableArray()
for ano in 1970...2016
{
dataAno1.addObject("\(ano)")
}
pickerTextFieldInicio.inputView = pickerViewInicio
My question is, how can I populate pickerTextFieldInicio with something like 31/10/2015?
I checked some posts realeted to Objective C, but I could not solve my problem as I'm using Swift.
Thanks.
Add a UIDatePicker to the MainStoryBoardand then create an #IBOutlet for the UIDatePicker in the ViewController.swift name it whatever you want for instance we will use diaPicked
Next create an #IBOutlet for an UILabel and name it.
Create a #IBAction for the DatePicker and name it. For intance let's say that we called it pickerAction Inside of the pickerAction add
#IBAction func PickerAction(sender: AnyObject) {
var diaFormato = NSDateFormatter()
diaFormato.dateFormat = "dd-MM-yyyy HH:mm"
var strDate = diaFormato.stringFromDate(diaPicked.date)
self.selectedDate.text = strDate
}
I see that your variables are in spanish so I'll translate it for you since I speak spanish,
Agrega un UIDatePicker al MainStoryBoard y crea un #IBOutlet para el UIDatePicker en el ViewController.swift y lo puedes llamar lo que quieras pero por este ejemplo vamos a user Dias. y despues crea otro IBOutlet y le llamas diaSelecionado
Después crea un #IBAction para Dias y le llamas diasSelecionadoAcion que salga del UIDatePicker llamado Dias
#IBAction func Dias (sender: AnyObject) {
var diaFormato = NSDateFormatter()
diaFormato.dateFormat = "dd-MM-yyyy HH:mm"
var strDate = diaFormato.stringFromDate(diaPicked.date)
self.diaSelecionado.text = strDate
}

"'ViewController.Type' does not have a member named . . . "

I have created two variables that are floats:
var yourWeight = (yourWeightTextField.text as NSString).floatValue
var calorieNumber = (calorieNumberTextField.text as NSString).floatValue
I got the following errors:
// 'ViewController' does not have a member named 'yourWeightTextField'
// 'ViewController' does not have a member named 'yourWeightTextField'
I have set my IBOutlets:
class ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var calculatorButton: UIButton!
#IBOutlet weak var inspirationLabel: UILabel!
#IBOutlet weak var beginningLabel: UILabel!
#IBOutlet weak var calculatorContainer: UIView!
#IBOutlet weak var answer1Label: UILabel!
#IBOutlet weak var doneButton: UIButton!
#IBOutlet weak var yourWeightTextField: UITextField!
#IBOutlet weak var exerciseListPickerView: UIPickerView!
#IBOutlet weak var calorieNumberTextField: UITextField!
#IBOutlet weak var menuExampleButton: UIButton!
#IBOutlet weak var aboutButton: UIButton!
#IBOutlet weak var calculateButton: UIButton!
Here's my ViewDidLoad Method:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib
yourWeightTextField.delegate = self
calorieNumberTextField.delegate = self
exerciseListPickerView.delegate = self
exerciseListPickerView.dataSource = self
calculateButton.enabled = false
// Calling the textfield valueChanged Methods
yourWeightTextField.addTarget(self, action:"yourWeightEditingChanged:", forControlEvents:.EditingChanged);
calorieNumberTextField.addTarget(self, action:"calorieNumberEditingChanged:", forControlEvents:.EditingChanged);
}
Here's the rest of the code:
var yourWeight = (yourWeightTextField.text as NSString).floatValue
var calorieNumber = (calorieNumberTextField.text as NSString).floatValue
var exerciseCurrentValue:Float = 0.009
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
switch (row) {
case (0):
exerciseCurrentValue = 0.009
case (1):
exerciseCurrentValue = 0.019
case (2):
exerciseCurrentValue = 0.023
case (3):
exerciseCurrentValue = 0.029
case (4):
exerciseCurrentValue = 0.045
case (5):
exerciseCurrentValue = 0.033
case (6):
exerciseCurrentValue = 0.038
case (7):
exerciseCurrentValue = 0.038
case (8):
exerciseCurrentValue = 0.039
case (9):
exerciseCurrentValue = 0.053
case (10):
exerciseCurrentValue = 0.061
case (11):
exerciseCurrentValue = 0.063
case (12):
exerciseCurrentValue = 0.063
case (13):
exerciseCurrentValue = 0.064
case (14):
exerciseCurrentValue = 0.076
case (15):
exerciseCurrentValue = 0.083
default:
exerciseCurrentValue = 0.009
}
}
#IBAction func calculateButtonTapped(sender: AnyObject) {
answer1Label.text = "It will take you \((exerciseCurrentValue) / ((yourWeight) * (calorieNumber))) minutes to burn off those calories by performing that exercise."
}
I also got another error at
#IBAction func calculateButtonTapped(sender: AnyObject) {
answer1Label.text = "It will take you \((exerciseCurrentValue) / ((yourWeight) * (calorieNumber))) minutes to burn off those calories by performing that exercise."
, but I assume this will be fixed:
// 'ViewController' does not have a member named 'yourWeight'
Please provide as much code possible because I am new to programming. Thank you!
You cannot assign properties with instance values.
You need to define your properties like this.
var yourWeight:Float = 0 // Or whatever default value you want
var calorieNumber:Float = 0 // Or whatever default value you want
Then in the viewDidLoad and wherever else you need to get the values, do:
self.yourWeight = (self.yourWeightTextField.text as NSString).floatValue
self.calorieNumber = (self.calorieNumberTextField.text as NSString).floatValue

Resources