I'm just learning how to build apps using Swift and Xcode. I'm following along through the Xcode Fundamentals book, and ran into an issue in the "Apple Pie" project. When you tap a button, the button is supposed to reference the button title and set a variable to the button title, but the title instead comes through as "nil" and the app crashes, even though a title is set in interface builder.
I created a basic new app to test how I think this code should be working - see screenshots.
When the button (title: Aaron) is tapped, the console should print the button title, but instead I'm getting the "nil" value. What am I missing here?
Post
Replies
Boosts
Views
Activity
This is driving me nuts. I have a UITableView w/ constraints =0 to leading, trailing, and top/bottm of safe area. The tableview is constrained exactly the same was as other TableViews in my app- but on this controller specifically it shifts upwards so the cell is butted up against the navigation bar. Any ideas as to what's causing this??
Should spacing should look like this. Gray space between navigation bar and TableViewCell.
Error state - no gap between navigation bar and tableview cell.
Hello,
I'm trying to display/hide a UIDatePicker embedded in a UITableViewCell. My intention is that the datePicker will start hidden. Then when the user selects the startDate label in the cell above the date picker, the cell's row hight will adjust, making the datePicker visible. Then when the user taps the startDate label again, the datePicker hides. They should be able to do this indefinitely.
In this code, the datePicker starts out hidden (as intended), shows when the user selects the StartDate label (as intended) and then hides when the user selects the startDate label again (as intended). However, if the user tries to show the datePicker for a 2nd time, nothing happens.
video link:
https://youtube.com/shorts/blu1OCBOCiE?feature=share
class AddEditLogbookTableViewController: UITableViewController {
@IBOutlet var nameTextField: UITextField!
@IBOutlet var descriptionTextField: UITextField!
@IBOutlet var startDateLabel: UILabel!
@IBOutlet var startDateDatePicker: UIDatePicker!
var logbook: Logbook?
//Couple variables related to displaying/hiding the date picker
var startDatePickerCellIndexPath = IndexPath(row: 3, section: 0)
var startDateLabelCellIndexPath = IndexPath(row: 2, section: 0)
var isStartDatePickerVisible: Bool = false {
didSet {
startDateDatePicker.isHidden = !isStartDatePickerVisible
}
}
init?(coder: NSCoder, logbook: Logbook?) {
self.logbook = logbook
super.init(coder: coder)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
updateDateView()
}
@IBAction func startDatePickerValueChanged(_ sender: UIDatePicker) {
updateDateView()
}
func updateDateView() {
var dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateStyle = .medium
return dateFormatter
}()
startDateLabel.text = dateFormatter.string(from: startDateDatePicker.date)
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
switch indexPath {
case startDatePickerCellIndexPath:
if !isStartDatePickerVisible {
print("indexPath: \(indexPath) row height: 0 if")
return 0
}
else {
print("indexPath: \(indexPath) row height: \(UITableView.automaticDimension) if/else")
return UITableView.automaticDimension }
default:
print("indexPath: \(indexPath) row height: \(UITableView.automaticDimension) default")
return UITableView.automaticDimension
}
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
if indexPath == startDateLabelCellIndexPath {
isStartDatePickerVisible.toggle()
print("isStartDatePickerVisible Toggled. Value: \(isStartDatePickerVisible)")
} else { return }
tableView.beginUpdates()
tableView.endUpdates()
}
}
The print statements output as follows.
Tap 1:
indexPath: [0, 1] row height: -1.0 default
indexPath: [0, 1] row height: -1.0 default
indexPath: [0, 0] row height: -1.0 default
indexPath: [0, 0] row height: -1.0 default
indexPath: [0, 2] row height: -1.0 default
indexPath: [0, 2] row height: -1.0 default
indexPath: [0, 3] row height: -1.0 if/else
indexPath: [0, 3] row height: -1.0 if/else
Tap 2:
indexPath: [0, 1] row height: -1.0 default
indexPath: [0, 1] row height: -1.0 default
indexPath: [0, 0] row height: -1.0 default
indexPath: [0, 0] row height: -1.0 default
indexPath: [0, 2] row height: -1.0 default
indexPath: [0, 2] row height: -1.0 default
indexPath: [0, 3] row height: 0 if
Tap 3:
indexPath: [0, 1] row height: -1.0 default
indexPath: [0, 1] row height: -1.0 default
indexPath: [0, 0] row height: -1.0 default
indexPath: [0, 0] row height: -1.0 default
indexPath: [0, 2] row height: -1.0 default
indexPath: [0, 2] row height: -1.0 default
Tap 4:
indexPath: [0, 1] row height: -1.0 default
indexPath: [0, 1] row height: -1.0 default
indexPath: [0, 0] row height: -1.0 default
indexPath: [0, 0] row height: -1.0 default
indexPath: [0, 2] row height: -1.0 default
indexPath: [0, 2] row height: -1.0 default
When I click "get" and then "install", it says "there is not enough disk space available to install the product".
Xcode on the app store says it's 12.5 Gb. When I go to About This Mac > Storage I have over 43 Gb available. What am I missing here?
My mac is fully up to date, Big Sur 11.6. Mid 2014 13" MbPro.