Clearing out UITableView - swift

I need a way to clear out a UITableView. Btw, I can't just clear out the data source and reload the UITableView. I have to remove all the cells from the UITableView.

I can't just clear out the data source and reload the tableview
Well, that's too bad, because that's the answer. Clear out the data source, or at least throw a flag that causes numberOfSections to return 0 - and then reload the table view.
I have to remove all the cells from the tableview
Yup, well, that's exactly what will happen if you do what I just said.

Try the following code:
let cell = tableView.cellForRow(at: NSIndexPath(row: z, section: 0) as IndexPath) as! Cusotmcell
cell.contentView.removeFromSuperview()
Where z is index of cell

Related

How to turn on editing in cell Swift 3

I have a question about cell in swift. Let's imagine that I have a cell with 20 rows. The cell is selectable right now, but the thing I want is that, when user selects 20 row it should be editable. And the value which enters the user should be saved in some variable. Is it somehow possible? Thank's in advance!
If you want 20 cells in tableView, then
You have to make nib of a cell, and make textfield in cell, and also create outlet in nib, (you can also create prototype cell)
Then in cellForRow AtIndexPath makes textField delegate as self (to class), in that class you have to implement textField delegate, and then you can access textFields as:
let indexPath = IndexPath(row: 0, section: 0)
let cell = tableView(tableView cellForRowAt: IndexPath) as! MYCELL
let text = cell.textField.text
Sorry for my bad english,
I think it will be helpful

Swift - read above cell value in tableview

Basically I am trying to get a value from a custom table view cell, as I have made 5 prototype cells - each with a different tableViewCell class. However, what I am trying to do is to read the data from the cell above. I have called my classes: TableViewCell1, TableViewCell2 etc... So on the cellForRowAtIndexPath, I have the code like this:
if indexPath.row == 1 {
let cell2 = tableView.dequeueReusableCellWithIdentifier("secondCell", forIndexPath: indexPath) as! TierCell2
return cell2
}
This works, but I have also tried to do it like this, to be able to access the value above (with no luck) - which only results in an optional value error.
let cell1:TableViewCell1 = TableViewCell1()
cell2.PriceText.text = cell1.PriceText.text
Any ideas on how I might accomplish this?
I suggest you then try creating one custom table view cell with the 5 price tier labels that you want to display. Then in the cellForRowAtIndexPath you just deque a cell as the custom class and fill in all the tier information that you already have in your dictionary and return that cell.

How to set the table view cell to adjust to label?

I am using Xcode to make an app. Using auto layout, I have a tableview contained in a container view which is part of a view controller embedded un a navigation controller.
This table view is my content page, so the individual tableview cells leads to another tableview which contains bulk of the information I would like to display.
I added UIlabel which contains a paragraph of words to each tableview cells and set the constraints to the respective tableview cells.
In the storyboard, it looks alright having to be able to see all the paragraphs. I have set the line to be 0, and word wrap.
When I run the programme with lets say 4s simulator, the table view cells do not display the whole paragraph but rather, just indicate "..." at the end of each paragraph.
How do I set the settings such that the tableview cells will adjust itself according to the UIlabel after being word wrapped giving a certain screen sizes of phone?
PS: I am new to SWIFT programming and thanks for taking your time to help me.
Have you done this?:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
This is required for self-sizing table view cells. Also make sure of the following things:
a) The number of lines of UILabel is set to 0
b) The autolayout system in the table view cell is proper -- all the views are pinned on all sides and the autolayout can accurately determine the height of the cell.
c) Also do a tableView.estimatedRowHeight = 75 (or your estimated height) for efficiency.
add this two line in viewDidLoad Method and label number of lines 0 and dont give any height.
func viewDidLoad() {
super.viewDidLoad()
tableView.estimatedRowHeight = 44.0
tableView.rowHeight = UITableViewAutomaticDimension
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CustomCell
//cell.customTexLabel Number 0f lines 0 and dont give height contrains just Leading,Trailing,TOP and Bottom.
cell.customTextLable?.text = "Here index starts from 0 which means first element can be accessed using index as 0, second element can be accessed using index as 1 and so on. Let's check following example to create, initialize and access arrays:Here index starts from 0 which means first element can be accessed using index as 0, second element can be accessed using index as 1 and so on. Let's check following example to create, initialize and access arrays:Here index starts from 0 which means first element can be accessed using index as 0, second element can be accessed using index as 1 and so on. Let's check following example to create, initialize and access arrays:"
return cell
}
Hi #Daniel you can learn how to create self sizing cell here is the demo tutorial for custom dynamic cell
http://developerclouds.com/2016/03/23/table-view-custom-cell-with-self-sizing/

Use tags for tableView inside UIViewController to update Labels

I added a table view to my UIViewController. then i added a label to my custom cell inside the table view. i tried to update labels inside the tableview by using tags. seems it does not working.
this is my code.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath)
let object = lineItemsObject[indexPath.row]
//cell.textLabel?.text = object["quantity"]
print(object["description"]!)
if let descriptionLabel = cell.viewWithTag(500) as? UILabel {
descriptionLabel.text = object["description"]
}
else
{
print("fail")
}
return cell
}
When i call the function always it does not read the tag and prints "fail". i have assigned correct tag value to my label also.
here i have attached a image of my label details in attribute inspector
Please help me the fix the issue.
As #TheAppMentor said code looks fine. Make sure you entered correct cell identifier. Then make sure you connected your view controller to correct class (it's my common mistake). Also make sure that something else in this cell hasn't the same tag. You can also print all subviews to check what's inside.
Your cell is not knowing about your label inside (the one with 500 tag). You are not providing a custom cell, because of this line:
let cell: UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath)
This is a predefined cell. As of documentation you can have predefined cells or custom ones (obtained by subclassing UITableViewCell):
"When creating cells, you can customize them yourself or use one of several predefined styles. The predefined cell styles are the simplest option. With the predefined styles, the cell provides label and image subviews whose positions and styling are fixed. ... To set the text and images of the cell, use the textLabel, detailTextLabel, and imageView properties."
If you want to go predefined:
If you want to just put the text onto cell use (I see this one is commented on first place).
cell.textLabel?.text = object["quantity"]
Go custom
Extend UITableViewCell on a separate swift file. Do your bindings here and work with your storyboard in parallel. Assign the custom class to your cell. Also, change dequeue:
let cell: MyCustomTableViewCell = self.tableView.dequeueReusableCellWithIdentifier("LineItemCell", forIndexPath: indexPath) as! MyCustomTableViewCell

Setting re-ordering property of a cell to NO

Can we specifically set the re-ordering property of a cell (Add cell with + button) to NO? I cannot set editingStyle to NO as then I would not get even the + button in Add cell.
Use the delegate method: tableView:canMoveRowAtIndexPath:.
You should use it by checking to see what the indexPath is and then return NO if the indexPath is the indexPath of a cell you do not want reorderingEnabled on.
You can set a cell's showsReorderControl to NO, implement tableView:moveRowAtIndexPath:toIndexPath:and return NO for tableView:canMoveRowAtIndexPath:.
http://developer.apple.com/library/ios/documentation/uikit/reference/UITableViewCell_Class/Reference/Reference.html#//apple_ref/occ/instp/UITableViewCell/showsReorderControl