2 days ago I started developing an APP as a school project with Swift and Storyboard.
Now I've started to use UITableViews and also have Cells design, but somehow they are generated and reduced in size and I don't understand exactly why.
How i designed it:
How it looks:
I wrote these costum subclass for the Cell:
import UIKit
class ContentView: UITableViewCell {
#IBOutlet weak var CellPriority: UILabel!
#IBOutlet weak var FromCell: UILabel!
#IBOutlet weak var CellTime: UILabel!
#IBOutlet weak var CellDesc: UILabel!
#IBOutlet weak var CellTitle: UILabel!
#IBOutlet weak var CellImage: UIImageView!
}
and this is my ViewController.swift
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{
#IBOutlet weak var ContentTable: UITableView!
var ContentArray = [ContentView]()
override func viewDidLoad() {
super.viewDidLoad()
ContentTable.dataSource = self
ContentTable.delegate = self
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 6
}
var titleArray = ["asd", "bsd", "csd", "dsd", "esd", "fsd", "gsd", "hsd", "isd", "jsd", "ksd", "lsd", "msd", "nsd", "osd", "psd", "qsd", "rsd", "ssd", "tsd", "usd", "vsd", "wsd", "xsd", "ysd", "zsd"]
var descArray = ["asd", "bsd", "csd", "dsd", "esd", "fsd", "gsd", "hsd", "isd", "jsd", "ksd", "lsd", "msd", "nsd", "osd", "psd", "qsd", "rsd", "ssd", "tsd", "usd", "vsd", "wsd", "xsd", "ysd", "zsd"]
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = ContentTable.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! ContentView
let image = UIImage(named:"")
cell.CellImage.image = image
cell.CellTitle.text = titleArray[indexPath.row]
cell.CellDesc.text = descArray[indexPath.row]
cell.CellTime.text = "resultE"
cell.CellPriority.text = "!!!"
cell.FromCell.text = "lol"
return cell
}
}
(I have everything else which is unnecessary, regarding the problem, has been deleted)
(These were things like the "erstellen" button and so on.)
Thank you very much
Related
I Have to do an app for recipes and it shows me different recipes in my tableView, and i just want to implement my CustomCell (from a xib file) to my storyboard and I don't know how to connect it to show my data (I already checked my identifier) here's the code of my controller :
class SearchRecipe: UIViewController, ShowAlert {
var recipeData = RecipeDataModel()
var recipe = [String]()
#IBOutlet weak var tableViewSearch: UITableView!
override func viewDidLoad() {
self.tableViewSearch.rowHeight = 130
}
func updateRecipeData(json: JSON){
if let ingredients = json["hits"][0]["recipe"]["ingredientLines"].arrayObject{
recipeData.ingredientsOfRecipe = ingredients[0] as! String
recipeData.cookingTime = json["hits"][0]["recipe"]["totalTime"].stringValue
recipeData.recipe = json["hits"][0]["recipe"]["label"].stringValue
recipeData.recipeImage = json["hits"][0]["recipe"]["image"].stringValue
}
else {
print("Problem")
}
//self.tableViewSearch.reloadData()
}
}
extension SearchRecipe: UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return recipe.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "customRecipeCell", for: indexPath) as! CustomRecipeCell
getRecipesDisplay(in: cell, from: recipeData , at: indexPath)
return cell
}
func getRecipesDisplay(in cell: CustomRecipeCell, from recipeModel: RecipeDataModel, at indexPath: IndexPath){
cell.recipeTitle.text = recipeData.recipe
cell.recipeInfos.text = recipeData.ingredientsOfRecipe
cell.timerLabel.text = recipeData.cookingTime
}
}
and this is the code my xib file :
class CustomRecipeCell: UITableViewCell {
#IBOutlet weak var recipeTitle: UILabel!
#IBOutlet weak var recipeInfos: UILabel!
#IBOutlet weak var cellImageBackground: UIImageView!
#IBOutlet weak var likeAndTimerView: UIView!
#IBOutlet weak var likeImage: UIImageView!
#IBOutlet weak var timerImage: UIImageView!
#IBOutlet weak var likeLabel: UILabel!
#IBOutlet weak var timerLabel: UILabel!
#IBOutlet weak var activityIndicator: UIActivityIndicatorView!
override func awakeFromNib() {
super.awakeFromNib()
activityIndicator.isHidden = true
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
In ViewDidLoad, you must register your cell this way:
override func viewDidLoad() {
tableViewSearch.register(UINib(nibName:" /* NAME OF YOUR XIB FILE */ ", bundle: nil), forCellReuseIdentifier: "customRecipeCell")
}
You also have to edit the size of your cell in the attribute inspector of your custom cell and of your table view
I have a tableView as a part of Stack View, but it doesn't appears on the screen
Here is the view:
Here I define the cells
class MealViewController: UIViewController, UITextFieldDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate,
UITableViewDelegate, UITableViewDataSource{
let imagePickerController = UIImagePickerController()
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var photoImageView: UIImageView!
#IBOutlet weak var ratingControl: RatingControl!
#IBOutlet weak var saveButton: UIBarButtonItem!
#IBOutlet var tableView: UITableView!
let list = ["Milk", "Honey", "Bread", "Tacos", "Tomatoes"]
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return(list.count)
}
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = UITableViewCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return(cell)
}
And also I override the viewDidLoad() method with this:
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
}
The structure of view looks like this:
where Table View itself has the following setting:
I don't get any errors and build is successful, but still I can't see the table on the screen (all other elements are presented), what could be the problem?
I would like to be able to preload the items in my collection view when it is being scrolled up and down.
I am using the data of two arrays to load the data in the collection view:
(1) availableLabelNames: [String]
(2) availableImageNames: [String]
Collection View functions in AvailableImages:
class availableImages: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource
{
#IBOutlet weak var searchBar: UISearchBar!
#IBOutlet weak var collectionView: UICollectionView!
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return availableLabelNames.count
}
public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as! CustomCollectionViewCell
cell.borderFolder.layer.borderColor = UIColor.black.cgColor
cell.borderFolder.layer.borderWidth = 2
cell.borderFolder.layer.cornerRadius = 5
cell.squareDesign.layer.cornerRadius = 5
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(availableImages.connected(_:)))
cell.imageCell.isUserInteractionEnabled = true
cell.imageCell.tag = indexPath.row
cell.imageCell.addGestureRecognizer(tapGestureRecognizer)
//setting image
cell.imageCell.image = UIImage(named: availableImageNames[indexPath.row])
//setting label
cell.labelName.text = availableLabelNames[indexPath.row]
return cell
}
}
Custom collection view class:
import UIKit
protocol Normal: class
{
func delete (cell: CustomCollectionViewCell)
func pressed (cell: CustomCollectionViewCell)
func textChanged (cell: CustomCollectionViewCell)
func finishedEditing (cell: CustomCollectionViewCell)
func textStartedEditing (cell: CustomCollectionViewCell)
}
class CustomCollectionViewCell: UICollectionViewCell
{
#IBOutlet weak var imageCell: UIImageView!
#IBOutlet weak var deleteButton: UIButton!
#IBOutlet weak var cellButton: UIButton!
#IBOutlet weak var labelName: UILabel!
#IBOutlet weak var textFieldName: UITextField!
#IBOutlet weak var squareDesign: UIView!
#IBOutlet weak var borderFolder: UIView!
weak var delegate: Normal?
//...class goes on but not important
Please could someone help me I have looked everywhere but nothing seems to work or makes sense in terms of my application.
Thank you so much! :)
Collection view is doing preloading by default. Your problem is elsewhere, you can't do heavy work in cell for item.
Would need a sample project to try it on, but I would bet this is the root of the problem:
cell.imageCell.image = UIImage(named: availableImageNames[indexPath.row])
rewrite it to this:
DispatchQueue.main.async { [weak self] in
guard let weakSelf = self else { return }
cell.imageCell.image = UIImage(named: weakSelf.availableImageNames[indexPath.row])
}
Also, as cells are reused, you want to reset their image. Otherwise, images will be shown randomly.
I read the other topics, but not solves my problem, I create a tableview and your cells with same value to make tests, but when I execute my project nothing data is showed. I put 5 size of rows to only to test. I don't know why my code not works.
My storyboard:
TableViewCellVenda.swift
class TableViewCellVenda: UITableViewCell {
#IBOutlet weak var cellView: UIView!
#IBOutlet weak var imageCloud: UIImageView!
#IBOutlet weak var labelValorTotal: UILabel!
#IBOutlet weak var labelQtdItens: UILabel!
#IBOutlet weak var labelCliente: UILabel!
#IBOutlet weak var labelDataVenda: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
ViewControllerVendas.swift
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
// UI View
#IBOutlet weak var tableViewVendas: UITableView!
#IBOutlet weak var waitView: UIActivityIndicatorView!
// DB
var db: OpaquePointer?
var count : Int = 0
// Data
var saleOrders : [SaleOrder] = []
override func viewDidLoad() {
super.viewDidLoad()
self.waitView.startAnimating()
self.waitView.hidesWhenStopped = true
tableViewVendas.delegate = self
tableViewVendas.dataSource = self
}
// TableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellVenda") as! TableViewCellVenda
print("Cell for row")
cell.labelCliente.text = "Nome do Cliente"
cell.labelDataVenda.text = "14/02/1991"
cell.labelQtdItens.text = "20"
cell.labelValorTotal.text = "R$ 542,22"
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 75
}
}
Emulator:
Try adding a new swift UIKit file with a class of type UITableView and do all the tableview setup in there. And of coarse set the tables’s class as this UITableView class.
I've seen some answers on Stack about this question but a lot of them are from 5 years ago and using an older version of Swift. I would like to reload the data in the table every time the user hits save so that the appended event will now be added to the table. But no matter what I seem to do nothing is showing up!
I've tried adding table.reloadData() in my save function. But i get an error for:
Thread 1 error, Fatal error: unexpectedly found a nil while unwrapping an optional value.
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
#IBOutlet weak var txtTitle: UITextField!
#IBOutlet weak var txtLocation: UITextField!
#IBOutlet weak var txtDate: UITextField!
#IBOutlet weak var txtTime: UITextField!
var eventsArray = [Event]()
#IBAction func btnSave() {
let event = Event(tits: txtTitle.text!, locs: txtLocation.text!)
eventsArray.append(event)
table.reloadData()
print(eventsArray)
}
#IBOutlet weak var table: UITableView!
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return eventsArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! CustomeCell
cell.title.text = eventsArray[indexPath.row].title
cell.location.text = eventsArray[indexPath.row].location
return cell
}
}
Did you set tableView's delegate on viewDidLoad?
table.delegate = self