Swift TableView in a UIView not displaying data
try this:
override func viewDidLoad() {
super.viewDidLoad()
// Add this
tableView.delegate = self
tableView.dataSource = self
}
tableView not showing data values
You will need to call the following methods before it listens to cellForRowAt
:
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 1
}
another common problem is that the frame of the table is not set (e.g. inside a UITableViewCell, with automatic dimensions). The table requires a frame before it gets cells.
TableView does not show data
Swift solution:
tableView.reloadData()
If you are calling it not from the main thread:
DispatchQueue.main.async {
self.tableView.reloadData()
}
How to fix Table View Cells not showing on launch?
Try debugging first that if the table view delegate and data source methods are called or not. Then reloading the table view after the API response is stored in the array. After this, self.items.append(show).
Try this.
switch response.result {
case .success(let shows):
print("Succes: \(shows)")
shows.forEach { show in
self.items.append(show)
}
self.tableView.reloadData()
case .failure(let error):
print("Eror: \(error)")
}
Javafx TableView not showing data
You need to actually tell the TableView
HOW to display the data. This is done using a CellValueFactory
. Basically, you need to tell each column of the table what type of data it holds and where it gets that data from.
You need to start by defining your columns (give them an fx:id
either in the FXML file or in SceneBuilder):
@FXML
TableColumn<Parameter, String> colCategory;
@FXML
TableColumn<Parameter, String> colSubCategory;
@FXML
TableColumn<Parameter, String> colSubSubCategory;
Each TableColumn
takes two Type parameters. The first defines the object being displayed (Parameter
). The second is the data type for this column (all yours are String
).
Once the columns are defined, you need to set their CellValueFactory
in your initialize()
method:
colCategory.setCellValueFactory(new PropertyValueFactory<Parameter, String>("cat"));
colSubCategory.setCellValueFactory(new PropertyValueFactory<Parameter, String>("subCat"));
colSubSubCategory.setCellValueFactory(new PropertyValueFactory<Parameter, String>("subSubCat"));
Here you are telling each column where to find the data to be displayed. The last argument on the line, in the quotes, is the name of your property within the Parameter
object.
So, when JavaFX populates your table, it will takes these steps to populate each column (colCategory
, for example):
- Get the
CellValueFactory
forcolCategory
. - The factory is a
PropertyValueFactory
, so determine which class holds the property (in this case it is theParameter
class) - Look in the
Parameter
class for aString
property by the name of "cat" - Populate the column's cell with the value of the
cat
property.
iOS UITableView Not Showing
Try to refactor you code using a completion handler without using the delegation pattern.
in your network file
:
enum ApiError: Error {
case network(Error)
case genericError
case httpResponseError
case invalidData
case decoding
// you can handle your specific case
}
func network(completion: @escaping ( _ error: ApiError?, _ users: [GitHub]?)-> Void) {
let url = "https://api.github.com/users"
let request: URLRequest?
if let URL = URL(string: url) {
request = URLRequest(url: URL)
URLSession.shared.dataTask(with: request!) { result, response, error in
if let error = error {
completion(.network(error), nil)
return
}
guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
completion( .httpResponseError, nil)
return
}
guard let data = result else {
completion(.invalidData, nil)
return
}
do {
let decodedData = try JSONDecoder().decode([GitHub].self, from: data)
completion(nil, decodedData)
} catch {
completion(.decoding, nil)
}
}
.resume()
}
}
then inside your ViewController you can use it this way:
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
network.network { [weak self] error, users in
guard let self = self else { return }
if let error = error {
print(error)
return
}
DispatchQueue.main.async {
guard let users = users else { return }
self.users = users
self.tableView.reloadData()
}
}
}
if it still doesn't show and cellForRow
doesn't get called, you probably have a problem with your constraints and the tableView frame is zero (either height, width or both).
Try to debug setting a breakpoint inside numberOfRowsInSection
and then in your debug area po tableView
or just print the tableView and check if width or height is zero. it will be probably get called a few times. The first time the frame should be zero but at some point you should get a frame with height and width. If don't then check your constraints.
You can check my example which has a table view 375 x 641
Related Topics
How to Update a Coredata Entry That Has Already Been Saved in Swift
Uisearchbar Presented by Uisearchcontroller in Table Header View Animates Too Far When Active
Control Cursor Position in Uitextfield
How to Dynamically Change the Font Size in Auto Layout iOS
Including Custom Data into iOS Crash Dumps
Why the Skstorereviewcontroller Does Not Let Me Submit a Review
Type 'Viewcontroller' Does Not Conform to Protocol 'Uitableviewdatasource'
Swift Error:Signal Sigabrt How to Solve It
iPhone In-App Purchase Store Kit Error -1003 "Cannot Connect to Itunes Store"
Current Week Start and End Date
How to Enable Back/Left Swipe Gesture in Uinavigationcontroller After Setting Leftbarbuttonitem
Draw Dotted (Not Dashed!) Line, with Ibdesignable in 2017
Get Device Location (Only Country) in iOS
Identify New iPhone Model on Xcode (5, 5C, 5S)
Uilocalnotification Is Deprecated in iOS 10
How to Use Avfoundation to Crop a Video
Ios 8.1 Simulator Always Uses Us Keyboard Layout Despite German Hardware Keyboard