Initial lag for a cell with a UITextView - ios

Current Setup
I have a custom cell, loaded from a xib, where most of its space is covered by UITextview. Also that cell may have a few textviews. And there are few more elements (one UIView + 2 UILabels) inside of this cell.
The Problem
I tried removing all those views and lag stays even if I have only one textview. Also, the lag is happening only for the first time. Later on, when I scroll down, and run into another cell with a textview in it, the lag doesn't happen at all.
Additional Info
The thing with this custom cell is that a textview is added to a UIStackView. At the beginning, a stackview is empty, because I don't know (at development time) how many textviews may/should be there.
I am aware that this is another thing that might affect on performance, but I have solved it ( I guess as best as it could) by checking how many textviews are already found in stackview's arrangedSubviews array when dequeuing a cell, and based on that info, I just add or hide views appropriately (rather than to destroying, and re-creating a required number of textviews each time).
I have tried using Instruments, but I didn't noticed that any of my classes take up CPU time, but rather some UIKit method calls that are called by the framework internally are the cause of this... If needed, I can post a screenshot, but I guess this is not relevant because those seem to be the usual system & framework calls. Plus I am testing on iPad 2 :D so maybe that is a thing (I have to optimize an app for slow devices).
Still, I guess I can optimize this somehow?
The MyCell class is rather simple (pseudo code):
class MyCell:UITableViewCell{
func configure(data:SomeData){
self.addOrHideViewsIfNeeded()
}
private func addOrHideViewsIfNeeded(){
//here, I check if stackview.arrangedSubviews has, and how many subviews are there, and
//add / hide them appropriately, means if I have to add them, I load them from the nib, otherwise, I reuse views from by adding them/removing them from a pool.
}
}
Also the lag is more noticealbe in Debug version in compare to Release version, which make sense, but it is still noticeable.

You might have to check if you are re-using the cells or not.
You can reuse it as below:
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NAIndexPath *) indexPath{
static NSString *cellIdentifier = #"Mycell";
cell = [tableView dequeueCellWithIdentifier:cellIdentifier];
if(cell == nil)
cell = [[MyCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: cellIdentifier];
}

Ok this is a sketch of my idea to do the preloading in an invisible from user perspective tableview row zero.
class MyCell : UITableViewCell {
static var initiallyPreloaded : Bool = false
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
if !MyCell.initiallyPreloaded {
//Do the initial preloading setup by adding UITextView to self.contentView
MyCell.initiallyPreloaded = true
} else {
//Setup the regular cell content otherwise
}
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
}
class ViewController: UIViewController , UITableViewDelegate {
func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell{
var cell: MyCell? = tableView.dequeueReusableCell(withIdentifier: "myCellIdentifier") as! MyCell?
if cell == nil {
cell = MyCell(style: .default, reuseIdentifier: "myCellIdentifier")
}
if indexPath.row == 0 {
//Display the initial cell in unnoticeable to user way (start with hidden/alpha zero)
//For this zero-th row the initial preloading should happen
}
return cell!
}
}

Related

dequeueReusableCellWithIdentifier creating a new cell instead of reusing one

I use a UITableView inside a UIViewController with custom cells. However, when dequeueReusableCellWithIdentifier is called, it sometimes creates a new cell instead of reusing an existing one.
For example: When the first cell gets updated for the first time, a new cell is created instead of reusing the original cell.
I have created a diagram to help better illustrate the problem. Each green rectangle is an action that should happen, and each red rectangle is one that shouldn't. Each arrow is a call of dequeueReusableCellWithIdentifier.
It's problematic when a UIButton is tapped (the action is called multiple times), and with a "swipe to delete" action, the cell which has been swiped can be covered by the new second cell, which makes it appear like the delete button is randomly disappearing.
How can I solve this problem?
EDIT:
code custom cell
class TableViewCell : UITableViewCell{
#IBOutlet weak var buttonStop: UIButton!
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
print("newCell")
backgroundColor = color
}
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
override func prepareForReuse() {
super.prepareForReuse()
}
var color = UIColor().randomColor()
}
code for extension randomColor
extension UIColor {
func randomColor() -> UIColor {
let aRedValue = CGFloat(arc4random()) % 255 / 255
let aGreenValue = CGFloat(arc4random()) % 255 / 255
let aBlueValue = CGFloat(arc4random()) % 255 / 255
return UIColor(red: aRedValue, green: aGreenValue, blue: aBlueValue, alpha: 1)
}
code for cellForRow
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("Cell",forIndexPath: indexPath) as! TableViewCell
cell.buttonStop.tag = indexPath.row
cell.buttonStop.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
return cell
}
buttonAction:
func buttonAction(sender: UIButton) {
print("action for cell : \(sender.tag)")
}
I reduced the code to the max , note after further investigation the button printed the same text because of one of my mistake I write the message twice ^^'
So the only problem remaining is for the "swipe to delete" gesture
If I understand correctly you have a problem with keeping (the result of) an action in sync with what is displayed in the tableView.
Understand how UITableView uses the cells: it will ask it's dataSource for a UITableViewCell. You can either create one every time, or ask the tableView for one to reuse. Because of this mechanism, a cell you create can appear in a certain position at one time, and another position later.
Make sure that your logic and data is independent of the actual tableViewCells at all times. So you should be able to do what you have to o with ONLY an NSIndexPath.
A UITableViewCell can give you a valid indexPath for a tableViewCell via one of two method -indexPathForCell: or indexPathForRowAtPoint:.
The way to handle actions on UITableViewCells is as follows:
Create a custom subclass of UITableViewCell
Create a protocol in this subclass (e.g. MyCellDelegate)
Give this class a property delegate of type id <MyCellDelegte>
When creating a cell in your dataSource, sign this dataSource to be the delegate of that cell.
Let this subclass handle the action (swipe / tap) by calling a method from your protocol on it's delegate (the delegate is your datasource).
In the implementation of that delegate method (on your datasource) you can call one of the methods on UITableView to get the indexPath. This is the point where you become independent on the actual tabelViewCell: use that indexPath to get data, or perform a certain operation.
Although this might seem like a lot of work for a small thing, it really is not that much work, and your code will be much more robust, easier to follow and less coupled.

Custom Cell TextView Repeat

I have been stuck for hours on this issue. I have created a custom cell inside a tableview which is inside a UIVC. The custom cell has it's own class file and is all linked up as well as having the buttons inside it connected to the UIVC via delegates.
Inside the custom cell there consists of a UITextView as well as some buttons.
Tapping the tableview adds a new custom cell. The user can type whatever their beautiful heart desires, eventually resigning the first responder of the textview. Here is my issue(s).
MAIN. First off. Overtime, the cells start mixing up the text that was typed in by the user, and starts reusing it. It becomes a mess.
Due to the fact I have a custom cell, I do not need to register the class in the viewDidLoad of the UIVC. A stack overflow answer stated.
Optional. Second. Eventually, after a certain point the keyboard blocks the cell view. I have no idea how to scroll the cell to the top of the tableview.
Optional. Third. This is just a bonus. How would one be able to save the data from a cell with a decent storage method. I heard NSUserDefaults is only good for small memory storage files. Basically. Transferring the data in the textview inside the custom cell, into another area (UIVC?) where the data can be saved. Most likely using an array and saving the data on that array.
Code. As requested.
UIVC
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let reminderCell = reminder_tableView.dequeueReusableCellWithIdentifier("cellReminder", forIndexPath:indexPath) as! addReminderCell
reminderCell.selectionStyle = .None
reminderCell.heightForReminderTextDelegate = self
reminderCell.delegate = self
reminderCell.reminder_textview.becomeFirstResponder()
return reminderCell
}
Custom Cell Code. Not all of it though.
import UIKit
import AVFoundation
//MARK: Height For Reminder Delegate
protocol HeightForReminderTextView
{
func heightOfTextView(height: CGFloat)
}
//MARK: Beginning of Class
class addReminderCell: UITableViewCell, UITextViewDelegate {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: nil)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
//MARK: Delegate Declarations
var heightForReminderTextDelegate :HeightForReminderTextView?
//MARK: Element IBOutlets
#IBOutlet var reminder_textview: UITextView!
#IBOutlet var reminder_cell_uiview: UIView!
//MARK: Button
#IBAction func non_priority_button(sender: AnyObject) {
println("pressed")
}
//MARK: Awake From Nib Starts Here
override func awakeFromNib() {
super.awakeFromNib()
reminder_textview.delegate = self
}
MAIN: When you dequeue a cell it means that cell may be reused it does not create a new cell everytime. For that UITableViewCell has a method named prepareForReuse() which you can override, where you can reset all your cell content. Ex:
override func prepareForReuse() {
myTextView.text = ""
}
Due to the fact I have a custom cell...
If you have only *.swift file for your custom cell you must registerClass(:forCellReuseIdentifier:)
If you also have *.xib file you should registerNib(:forCellReuseIdentifier:) in the viewDidLoad
Optional
You can use scrollToRowAtIndexPath(_:atScrollPosition:animated:) to scroll desired cell to top of tableView
Optional
You can use CoreData, JSON, some SQLite wrappers to save data. In NSUserDefaults you can use small amount of data, like some settings.
I recommend to use NSJSONSerialization for now. You can create NSData from a Dictionary and write data as *.json to Documents directory using NSFileManager. In your app you should keep an array or dictionary with all the data and pass to another view controller that data.
when user types text, you should save it to some array or dictionary accoding to your requirment and that would helpyou with resusing problem.
Now, second problem of yours is scrolling I answered this question hope this will help. this is in objective C although.
and third for transferring data from OneVc to another you can use Segues
Hope this helps

How can I order two dynamic table view cells in swift?

Greeting everyone!
I am a noob when it comes to swift or other programming language, but I do want to learn. I am working on an app that, basically, will display a page of Results in a dynamic tableview. I need the table to have two different type of cells (shown bellow with two identifiers: cell and kidcell). "Cell" is "static", its information is set in code and doesn't change. It also segues to a different view controller, showing the user other info. "kidcell" is changed dynamically according to the info passed to it from a different page.
My problems is that "kidcell" cells are shown on top of the "cell" one.
How can I reorder those two type of cells in code, in swift?
Since this is done only in code, the storyboard doesn't have any "prototype" cells in this table view. Nevertheless, I tried to create two, giving each one of the two identifiers I need, and arrange those, but that still doesn't work.
In storyboard, I also added different "tags" to the two cells, still no luck.
Please help me, I feel the solution is very simple but for the life of me I can't figure it out and I can't find it anywhere.
EDIT:
"cell" will never change, as I mentioned
"kidcell" cells will always change, the data in them and also the number of "kidcell" cells, depending on the data passed. I want:
cell - on top
kidcell - below, with whatever cells will be.
Thank you!
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if ((indexPath.section == 0 && indexPath.row == boys.count) || (indexPath.section == 1 && indexPath.row == girls.count)) {
var cell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("cell") as? UITableViewCell
if (cell == nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
cell?.accessoryType = .DisclosureIndicator
}
cell?.textLabel?.text = (indexPath.section == 0) ? "Boys" : "Girls"
return cell!
} else {
var kidcell: UITableViewCell? = tableView.dequeueReusableCellWithIdentifier("kidcell") as? UITableViewCell
if (kidcell != nil) {
kidcell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: "kidcell")
}
You need NSArray that holds all the cells in your tableview.
The first object should be "Cell", and the other should be "kidCell".
Just retrieve the cell in the cellForIndexPath by the index of the row.
The best idea would be to store the data needed to apply to the cells in an array and order the array before dynamically loading them into the cells.

Pass data to UITableViewCell when initialising

I have a variable that I need to pass from the main UITableView to the cell subview to use in the initialisation method. I tried doing this:
class CollectionCell : UITableViewCell {
var collectionElement : PFObject = PFObject(className:"CollectionElement")
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.collectionElementArray = (collectionElement["elementFileArray"] as? [PFObject])!
...
}
I then create the cell in this way:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
...
let cell : CollectionCell = tableView.dequeueReusableCellWithIdentifier("CollectionCell", forIndexPath: indexPath) as! CollectionCell
cell.collectionElement = collectionElements[indexPath.row]
return cell
}
The variable is not set in the init method.
As ncerezo says the cells are not getting created, they are getting recycled so no init functions get called. the better approach is to use didSet on your collectionElement variable and do whatever view updating based on the new value there after ensuring it isn't nil. Then when you set the property of the cell in tableView(tableView: , cellForRowAtIndexPath:) the cell will actually update itself.
var collectionElement : PFObject = PFObject(className:"CollectionElement") {
didSet {
// Update the cell's view elements here, rather than an init function
}
}
UITableView reuses cells, and it's responsible for creating them.
You are NOT creating the cell: as the method explains by its name, dequeueReusableCellWithIdentifier just gets an already created (and probably used) cell from the table view.
Imagine that, given your cell size and device screen, the tableView can show 10 rows at a time. It will create a few more, lets say 12, and then will call cellForRowAtIndexPath on the UITableViewDataSource every time it needs to fill a cell with new information to make it visible.
When you call dequeueReusableCellWithIdentifier, you get one of those already created cells, and you're responsible for updating the necessary elements on it to reflect the underlying data. You can thus not assume that any part of it will be "empty" or uninitialized.

How to add a button to a table view cell in iOS?

I'm creating a productivity app in Swift. I'm not using a prototype cell in the Storyboard as most of it has been written in code already. I'd like to a checkbox button.
How would I go about doing that?
While the answer from Tim is technically correct, I would not advise on doing this. Because the UITableView uses a dequeuing mechanism, you could actually receive a reused cell which already has a button on it (because you added it earlier). So your code is actually adding a 2nd button to it (and a 3rd, 4th, etc).
What you want to do, is create a subclass from the UITableViewCell which adds a button to itself, while it is being instantiated. Then you can just dequeue that cell from your UITableView and it will automatically have your button on it, without the need to do it in the cellForRowAtIndexPath method.
Something like this:
class MyCustomCellWithButton: UITableViewCell {
var clickButton = UIButton.buttonWithType(UIButtonType.Custom) as! UIButton;
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier);
self.contentView.addSubview(self.clickButton);
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
And then you can actually dequeue it in the cellForRowAtIndexPath like this.
var cell = tableView.dequeueReusableCellWithIdentifier("my-cell-identifier") as? MyCustomCellWithButton;
if (cell == nil) {
cell = MyCustomCellWithButton(style: UITableViewCellStyle.Default, reuseIdentifier: "my-cell-identifier");
}
return cell!;
Well, first of all your cellForRowAtIndexPath should probably use the dequeue mechanism so you aren't recreating cells every time when virtualizing.
But that aside, all you need to do is create the button, and add it as a sub view to the cell.
cell.addSubview(newButton)
But of course then you will have to manage the sizing and layout as appropriate.
A UITableViewCell also has a selected state and a didSelect and didDeselect method available that listens to taps on the whole cell. Perhaps that's a bit more practical since you seem to want to check/uncheck checkboxes, which is more or less the same as selecting. You could set the cell in selected state right after you dequeued it.

Resources