How to remove delete button when editing UITableViewCell? - ios

I want to customize my cell behavior - I want cell view to change and different buttons appear. I got half of it working - when I swipe I get the view to move.
But I have this delete button to the right side of cell, I want to get rid of it and use custom button instead of it. What should I use to remove it and where do I bind my button to do the same thing?
I tried using two methods, setEditing: and willTransitionToState, but that didn't help.
If I skip calling parent method in any of these, I don't get delete button as I want, but my view never returns to its original position after editing and other cells stop reacting to swipes. What do I need to do to fix this?
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:YES];
if (editing) {
[UIView animateWithDuration:0.3 animations:^{
self.parentView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 100, 0);
}];
} else {
[UIView animateWithDuration:0.3 animations:^{
self.parentView.transform = CGAffineTransformIdentity;
}];
}
}
-(void)willTransitionToState:(UITableViewCellStateMask)state
{
[super willTransitionToState:state];
if (state == UITableViewCellStateShowingDeleteConfirmationMask) {
[UIView animateWithDuration:0.3 animations:^{
self.parentView.transform = CGAffineTransformTranslate(CGAffineTransformIdentity, 100, 0);
}];
}
if (state == UITableViewCellStateDefaultMask) {
[UIView animateWithDuration:0.3 animations:^{
self.parentView.transform = CGAffineTransformIdentity;
}];
}
}

Since your cell is a subclass of UITableViewCell, and you don't want to use the stock delete you probably want to disable it. The bare basic way would be to return no editing style for any of the cells.
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleNone;
}
Some of the comments mentioned what you can do next.
Add an additional view that is below the main view of your cell.
Add a button on that view on the right in the same place the delete button was.
Use either a swipe gesture recognizer to do a basic swipe or a pan gesture recognizer
for dragging (normal behavior).
Update the top view frame based on the translations.

no you can't change the delete button that appears in default UITableViewCell.
Instead, create a customClass of UITableViewCell.
or,
Hack its subView and find any subView which is of class UIButton and title text "delete", then change it.

Related

Putting UISearchBar in a custom UITableview causing weird animation

Basically, I am including two buttons located below the navigation bar. And below these two buttons, there is a UITableView, with the UISearchBar as its header view. However, when I click the search bar, the animation moves very strange.
Then I try to use animation to move the UITableView together with the search bar to the top,
the animation goes like this
The code added to the table view is like this:
- (BOOL) searchBarShouldBeginEditing:(UISearchBar *)searchBar {
CGRect tableViewFrame = self.myTableView.frame;
tableViewFrame.origin.y = 0;
[UIView animateWithDuration:0.1
animations:^{
self.myTableView.frame = tableViewFrame;
}
completion:nil];
return YES;
}
I am wondering how to move the UISearchBar to the top of the screen, together with the whole table view with smooth animations.
- (BOOL) searchBarShouldBeginEditing:(UISearchBar *)searchBar {
[UIView animateWithDuration:0.1
animations:^{
[ searchBar setFrame:CGRectMake(searchBar.frame.origin.x, 0, searchBar.frame.size.width, searchBar.frame.size.height)];
[self.myTableView setFrame:CGRectMake(myTableView.frame.origin.x, searchBar.frame.size.height, self.myTableView.frame.size.width, myTableView.frame.size.height)];
}
completion:nil];
return YES;
}
try this code

CGRectOffset animation misbehaved when working with TextField

The task is to move the textfield up when user done editing.
Right now, I have a text field textfield in the centre of the view, and a method moveTextFieldToTheTop.
Here is the code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[_textField resignFirstResponder];
return NO;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self moveTextFieldToTheTop];
}
- (void)moveTextFieldToTheTop
{
[UIView animateWithDuration:0.3
animations:^{
_textField.frame = CGRectOffset(_textField.frame, 0, -100);
}];
}
Instead of moving 100px up from the centre, textfield would somehow appear 100px below the centre and move to the centre position.
I debugged the code, and find out
[_textField resignFirstResponder];; is the cause of this problem.
But I really can't find out why and how to solve this problem.
Can someone please help me?
Update
I was using auto layout when I came across this problem. If I uncheck use auto layout, problem solved.
But is there a way to solve this problem with auto layout checked?
With auto layout change your moveTextFieldToTheTop method with this:
- (void)moveTextFieldToTheTop
{
//TODO update _textField's constraint to move _textField 100pt up
[_textField setNeedsLayout];
[UIView animateWithDuration:0.3
animations:^{
[_textField layoutIfNeeded];
}];
}
if you are using storyboard, then connect constraint of _textField to view controller, and update the code TODO part with that constraint(you need to change constraint .const property by 100)
Update
Here is the example project with textfield, just run the example, touch on textfield to open keyboard, and then press return button on keyboard.
https://www.dropbox.com/s/x138ta2uy1z3bbo/ToolBarhighlightproblem.zip?dl=0

How do you have a cell stay highlighted until you pop the view controller it transitions to?

Many apps have this effect where you tap the cell, it transitions to a new view controller, and then when you pop that view controller go to back to the cells, the cell you tapped to get there fades out only then. Basically, it only fades out when you return to the view controller, giving you a really nice visual queue as to which cell you tapped.
It's visible in Mail.app, for instance.
How do I do this? It seems I can only make it fade immediately or not at all.
For example, you have two view controllers, ListViewController (Table one) and DetailViewController.
In ListViewController.h, add one param: selectedIndexPath (NSIndexPath type).
In ListViewController.m -> didSelectRowAtIndexPath, set its value to current indexPath
selectedIndexPath = indexPath;
Now when you tap back button from DetailViewController and come back to ListViewController, viewWillAppear method gets called from ListViewController.
ListViewController.m -> viewWillAppear
if (selectedIndexPath != nil) {
// 1. get the cell using cellForRowAtIndexPath method from selectedIndexPath position
// 2. now you can set cell.contentView.alpha = 0
// 3. write an animation block which will set alpha to 1 in particular time block, let say 1 sec.
[cell.contentView setAlpha:0.0f];
[UIView animateWithDuration:1
delay:1.0
options: UIViewAnimationCurveEaseOut // change option as per your requirement
animations:^{
[cell.contentView setAlpha:1.0f];
}
completion:^(BOOL finished){
NSLog(#"Done!");
}];
}
Update the animation block as per your requirement.
Hope this helps.

UIKeyboard Disabling Touch Events

When the keyboard is displayed in my app, no touches will work in my view. I will tap a UITextField and the keyboard will be presented. Now that it is displayed, I cannot tap on other textfields,buttons or select text even in the active textfield.
Any ideas what may cause this
Thanks.
UPDATE 1:
I have found that because I animate the view upwards a little when the keyboard is displayed this is causing a problem. I have the master, view controller view. Inside this is formContainerView which contains my two UITextFields and UIButton.
I animate formContainerView up so that my fields aren't hidden behind the keyboard. I run the following code to do this:
[UIView animateWithDuration:duration delay:0 options:animationOptionsWithCurve(curve) animations:^{
[self.formContainerView setFrame:CGRectOffset(self.formContainerView.frame, 0, -220.0f)];
}
completion:^(BOOL finished) { }];
So I just offset the view by 220 pixels. If I set this to 0, i.e. don't animate the position, all touches work. But by moving the view up, all touches fail to work.
Why?
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
if ([txtfld_firstname isFirstResponder])
{
[txtfld_firstname resignFirstResponder];
[txtfld_secondname becomeFirstResponder];
}
return YES;
}
first set to the delegate for the textfield.
and after that write the above method when you click on the first textfield and after that press return button on the keyboard this (textfield delegate)method call and your keyboard disable.

Toggling UITableViewCell selectionStyle causes labels on cell to disappear

I have a custom subclass of UITableViewCell. On my storyboard, it basically has a containerView (UIView *), that has some labels on it, and a UISwitch. I add a gesture to slide my tableViewCell to the right to show some detail controls underneath the containerView, and then another gesture to slide the original view back. The animation code looks like this:
- (void)showCustomControls:(UISwipeGestureRecognizer *)gesture {
[UIView animateWithDuration:0.3 animations:^{
[self.containerView setFrame:CGRectMake(self.frame.size.width - kTableViewCellSwipedWidth, 0, self.frame.size.width - kTableViewCellSwipedWidth, kOverviewTableViewCellHeight)];
}completion:^(BOOL finished) {
self.isReveal = YES;
self.selectionStyle = UITableViewCellEditingStyleNone;
}];
}
- (void)hideCustomControls:(UISwipeGestureRecognizer *)gesture {
[UIView animateWithDuration:0.3 animations:^{
[self.containerView setFrame:CGRectMake(-kTableViewCellBounceWidth, 0, self.frame.size.width, kOverviewTableViewCellHeight)];
}completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 animations:^{
[self.containerView setFrame:CGRectMake(-kTableViewCellBounceWidth / 2, 0, self.frame.size.width, kOverviewTableViewCellHeight)];
}completion:^(BOOL finished) {
[self.containerView setFrame:CGRectMake(0, 0, self.frame.size.width, kOverviewTableViewCellHeight)];
}];
self.isReveal = NO;
self.selectionStyle = UITableViewCellSelectionStyleBlue;
}];
}
I noticed if I left the selectionStyle on, with the row open, it looked kind of funny. So I wanted to turn the selectionstyle off. So the code in there is to turn on and off the selection style when the row is open or closed. It works, however the weird thing is, when hideCustomControls gets called, and my original row slides back into place, the labels on the container view all go away. I have a little edge of the original row that shows, and when that is showing, the labels are there. But the second that the row slides all the way in place, all the labels disappear. The UISwitch which is also on the containerView never has problems though. It is always present. Is there a reason why this may be happening? When I remove the selectionStyle code in both methods, this problem goes away. Thanks in advance.
Edit:
I put in the correct code to animate using NSLayoutConstraint. Now the problem is the UIView subclasses I have on the view underneath show through to the top view when my row slides back into place. Not sure why this happens...

Resources