Issue on UITableViewCell custom class and UITableView scroll - ios

I created a custom class of UITableViewCell with some variables for different state on UIGestureRecognizer.
For exemple :
class CustomClass : UITableViewCell {
var isDisabled : Bool! = false
}
I have a swipe gesture to detect that the user want disabled the cell. So when the user swipe, I set my variable "isDisabled" to true.
Everything works fine until here.
But, if the first row has the variable "isDisabled" to true and I scroll in my tableView, the first row load at the bottom of the page has the variable "isDisabled" to true too, but the user don't say that he want this cell disabled.
So, have you any idea how I can solved my problem ?
Thanks

The issue you explain is regarding the reuse cells bahaviour of the UITableView. You can solve in different ways, I'll try to explain you one of them:
You can create a collection (Array, or something else) of Bool for example, with the size of the numbers of cells in your UITableView and whenever you detect a swipe gesture mark the position in the array for the indexPath.row to true or false depends of it state. This keep the status of the cells for each index.
I hope this help you.

Related

Implementing different user interaction function for subviews of a cell

I have a UICollectionView which allows for a user to select a cell and upon doing so view 'A' will appear. I am wondering if it is possible for the subviews of this cell, ex: UIlabel and UIImageView to provide a different functionality for when they alone are selected. For example, if the UIImageView is selected, I want to segue to view 'B' as opposed to 'A'.
I have attempted to implement a UITapGestureRecognizer for both the label and the image, however, the cell's functionality overrules and the resulting view is still 'A'. Any ideas?
Thank you in advance.
What you want to achieve is possible through delegates if you don't have custom cell make a custom cell class then inside custom cell declare your protocol
I assume you have to disable the default behavior of the collection view cells:
cell.selectionStyle = UITableViewCellSelectionStyle.none
However, if you set that and you encounter an overlap issue, please take a look at the 'cancelTouchesInView' property of the 'UITapGestureRecognizer'. Basically, by setting that to false, you allow the children to also receive touch actions.
Furthermore, do not forget each gesture recognizer should have it's own method for you to be able to segue into two different places.

Reload table view on editing UITextView inside UITableViewCells

I have some customised UITableViewCells which contain a UITextView and a set of buttons. The buttons in the cell should be visible only when the user tries to edit the text view in the corresponding cell.
So, if the user attempts to edit the textview in cell1, then the set of buttons should be visible below the textview in cell1 and the height of cell1 should also be increased. Now, if the user attempts to edit the textview in cell2, then the set of buttons should be visible below the textview in cell2 and the height of cell2 should also be increased, whereas the buttons in cell1 should get removed and cell size needs to be calculated accordingly.
For this I tried to reload the table view cells from textViewDidBeginEditing:. This is reloading the cells properly and shows/hides the buttons properly in the required cells, but does not allow proper editing of text view. When the user tries to edit with the textview, the tableview reload methods are invoked constantly and not allowing the keyboard to stand for editing.
Is it right to handle reloading from textViewDidBeginEditing: in first place ? is there some better way to do this ? please help.
First I would suggest that do not reload the entire TableView each time. Instead use the
reloadRowsAtIndexPaths
method to load only the cell in which you want modifications.
Next, to solve your issue regarding the textView, you could do something like this, declare a class property of bool type, and set it to false. When you reload your cell for the first time, set it to true. Now in the textViewDidBegin editing method, check for this bool. If it is set to true, that means you already have loaded the cell and you do not need to load it again, so in this case do not call the reloadRows method. Else if it is false, reload the rows and set this bool to true.
Now in the textViewDidEndEditing delegate method, set this bool to false again so that when a user taps on another textView in some other row, it is reloaded properly.
This logic may not be perfect, you may require some tweaking. But it will get the job done
Explicitly make the textfield as firstResponder
if buttonsDisplayed == NO {
reload cell
}
if textFieldIsFirstResponder == NO {
[textField becomeFirstResponder];
}

Center Cell UITableView When Scrolling Is Finished

I have a UITableView, and I don't want it to stop between two cells when it stops after scrolling. So I need to move the cell which is the nearest of the Y center of the screen, when the velocity of the UITableView is less than a given value.
I'm sure I'm not the first who wants this effect, but I didn't find anything in my research. There must be a word for this that I don't know. Does someone have a link or something ?
I tried to do this on my own, but I don't find how to get the cell. I used tableView.indexPathsForVisibleRows(), so I have an array of indexPaths, but now I don't know how to call the corresponding cell. There is cellForRowAtIndexPath, but this method creates a new cell, doesn't it ?
You need to implement -scrollViewWillEndDragging:withVelocity:targetContentOffset: in your table view delegate, just as in this other question here.
Basically, this method allows you to tweak the position where the velocity scroll will halt.
I can't help you more with code without knowing the structure of your table view (cells of variable height?).
Try adding this to your ViewDidLoad:
self.tableView.bounces = false

Hiding objects within UITableViewCell iOS 8 (Swift)

I have a UITableViewCell that on touch, expands (dropdown). The user is then presented with a selection of options. When the user touches one of the options I want to briefly hide all the options, show an activity indicator spinning, display a confirmation message (on success) and then collapse the cell back to normal.
I'm having trouble hiding any object within the custom cell. The following simple code doesn't work (this is the correct superview corresponding to the cell):
var customCell: MyCustomCell = icon.superview.superview.superview as MyCustomCell
customCell.myLabel.hidden = false
I have tried hiding/showing a variety of different objects but nothing works. I've even popped it inside dispatch_async to ensure it runs on the main thread.
Additionally, Xcode 6 beta tells me that myLabel.hidden is read only. This also happens for other objects. Is this no longer the correct way to hide something?
I've had a search around but had no luck in finding an answer. If someone could help me out I'd be grateful! Thanks.
The usual way of doing something like this would be to implement the UITableViewDelegate method -tableView:didSelectRowAtIndexPath: to let you know when a row is tapped.
Then you can easily get and modify the cell
var customCell = self.tableView.cellForRowAtIndexPath(indexPath) as MyCustomCell
customCell.myLabel.hidden = false
I consider the line icon.superview.superview.superview to be a code smell. The multiple superview calls makes makes your code fragile and likely to break if you ever change the view hierarchy in your cell.

How to disable: Drag across UITableViewCell selects it

When you drag over a UITableViewCell in a TableView, the cell gets highlighted ("selected"), but didSelectRow... is not called. I wish to disable that selection. How would I do that? Note that setting the selectedStyle for a cell to selectedStyleNone, or something like that, is not what I want. I really want the OS not to select it.
Thanks in advance,
I think the property your searching for is BOOL allowsSelection declared in UITableView. By passing "no" the tableViewCell is not selected when the user touches it. The same is true for BOOL allowsSelectionDuringEditing.
I hope that fixes your problem ;)

Resources