UITableView indents at the wrong time - ios

I have a default edit button on the top left of my navigation bar. When it is pressed, the damn table does not show the indentation the first time. When I press "done" it does! How do i fix this?
Here is my code:
- (void)viewDidLoad {
self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
#pragma mark edit
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
[super setEditing:editing animated:animated];
[_tableView setEditing:editing animated:animated];
}
- (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
-(void) tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
}
EDIT
Overriding these two methods in the UITableViewCell subclass removes the indentation for me.
- (void)willTransitionToState:(UITableViewCellStateMask)state
- (void)didTransitionToState:(UITableViewCellStateMask)state
So I can now make my own animation.

Overriding these two methods in the UITableViewCell subclass removes the indentation for me.
- (void)willTransitionToState:(UITableViewCellStateMask)state
- (void)didTransitionToState:(UITableViewCellStateMask)state
So I can now make my own animation.

Related

Delete button disappeared on scrolling in edit mode of UITableview

I am facing the issue when I scroll the tableview in edit mode. It's because of reusability of cells. I followed the link-
UITableView in Edit Mode - Delete Button Dissapears on Scroll
Got nothing from that.
Here is my code snippet-
- (void)setEditing:(BOOL)editing
animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[tableView setEditing:editing animated:animated];
[tableView setAllowsMultipleSelectionDuringEditing:editing];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return NO;
}
- (BOOL)tableView:(UITableView *)tableview canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:#"Cell"];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"Cell"];
}
[cell setEditingAccessoryType:UITableViewCellAccessoryDetailDisclosureButton];
cell.textLabel.text=[NSString stringWithFormat:#"%ld",indexPath.row+1];
cell.detailTextLabel.text=#"";
cell.clipsToBounds = YES;
return cell;
}
So any recommendation or suggestion from anyone how to resolve that issue?
Thank you.
Change -(void)setEditing:(BOOL)editing animated:(BOOL)animated method into this:
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
_myTV.allowsMultipleSelectionDuringEditing = NO;
[super setEditing:editing animated:animated];
if(editing) {
[_myTV setEditing:YES animated:YES];
} else {
[_myTV setEditing:NO animated:YES];
}
}
- (BOOL)tableView:(UITableView *)tableview shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES; // return yes
}
Now run your program and let me know what happening?
edit
Get this demo from here. Nice exlanation is given

Get touch location in UITableViewCell

I am showing UIMenuController using UITableView delegates,
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(5_0){
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0){
if(action == #selector(copy:)){
return YES;
}
return NO;
}
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender NS_AVAILABLE_IOS(5_0){
if(action == #selector(copy:)){
NSLog(#"Action");
}
}
But Is there any way to get the touch location in the UITableViewCell?
The easiest way is: Create custom UITableViewCells which have clear (invisible) buttons over the "hot spots" you care about someone touching.

iOS app - delete buttons not showing when table view is in editing mode

I am very new to iOS but I've created a view controller with a table view which i want to put into editing mode. This is my view controller implementation file code:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize progsTable, programmes;
- (void)viewDidLoad
{
[super viewDidLoad];
programmes = [[NSMutableArray alloc] initWithObjects:#"one",#"two",#"three",nil];
//set the title
self.title = #"Programmes";
//add an edit button
self.navigationItem.leftBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[progsTable setEditing:editing animated:animated];
}
#pragma mark - UITableView Datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return programmes.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if(cell==nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [programmes objectAtIndex: indexPath.row];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *) indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle==UITableViewCellEditingStyleDelete) {
//remove from array
[programmes removeObjectAtIndex:indexPath.row];
//remove from table
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
#pragma mark - UITableView Delegate methods
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}
#end
I get the edit button in the top left but when i click it the red delete buttons don't appear - what am I doing wrong?
Running your exact code does work - the delete buttons are displaying when i test it.
I believe the problem is that you haven't assigned your #property called progsTable to your Table View.
To solve it do the following:
Go to your Storyboard
Right click on your View Controller
Click and hold the + next to the outlet called progsTable while you drag the mouse to your Table View, like this:
Try running your app - the delete buttons should now appear.
I think you need to add the code below:
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete; }
This tells the UITableView which editing style you want.
Use this
- (IBAction)deleteDrug:(id)sender event:(id)event {
selectedButtonIndex = [self indexPathForButton:sender event:event];
[tableView setEditing:YES animated:YES];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath == selectedButtonIndex) {
return YES;
}
return NO;
}
You may need to implement this method for your datasource.
- (UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
Check that you also have auto-layout constraints added in interface builder so that the very end of the cell lines up with the width of your device.
Run the code on the ipad in the simulator, if the delete button shows up there but not on the iphone simulator then it probably just that the contents are not aligned properly to the viewport of your device.
My 'delete' button was not showing up but the cell title content were moving over to the left when I swiped, however I had not set constraints on my cell width in my storyboard, as soon as I set the right-edge constraint to '0' or '16', the delete button appeared.

UITableView editing style may not show delete button

Occasionally, when UITableView is in editing style and when you trigger the delete button, this button may not show. It seldom happen, but did anybody get the same issue too?
Post some code:
In viewDidLoad,
[[self tableView] setEditing:YES animated:YES];
table view delegate:
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
//do sth.
}
}

show UIMenuController in UITableViewCell, grouped style

Is there a simple way to implement the copy menu when a cell is tapped, instead of subclassing the UITableViewCell?
thanks,
RL
In iOS 5, a simple way is to implement the UITableViewDelegate Methods:
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
By implementing the 3 delegates, it will enable call UIMenuController for you after a long press gesture. An example like:
/**
allow UIMenuController to display menu
*/
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
/**
allow only action copy
*/
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
return action == #selector(copy:);
}
/**
if copy action selected, set as cell detail text
*/
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
if (action == #selector(copy:))
{
UITableViewCell* cell = [tableView cellForIndexPath:indexPath];
[[UIPasteboard generalPasteboard] setString:cell.detailTextLabel.text];
}
}
Yes!
Call [[UIMenuController sharedMenuController] setMenuVisible:YES animated:ani] (where ani is a BOOL determining whether the controller should be animated) from within - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath ( UITableView's delegate method)
Edit: The 'copy' command on the UIMenuController will not by default copy the detailTextLabel.text text. However, there is a workaround. Add the following code into your class.
-(void)copy:(id)sender {
[[UIPasteboard generalPasteboard] setString:detailTextLabel.text];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if(action == #selector(copy:)) {
return YES;
}
else {
return [super canPerformAction:action withSender:sender];
}
}

Resources