iPhone:How to change default delete button's frame in UITableViewCell - iphone

I have facing problem is that Is there any way to change default frame
of delete button in UITableView and one more thing is that delete button is always display in center of cell of UITableViewcell so any way to change frame of delete button.
// Update the data model according to edit actions delete or insert.
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
[appDelegate.categories removeObjectAtIndex:indexPath.row];
[categoryTableView reloadData];
}
}
#pragma mark Row reordering
// Determine whether a given row is eligible for reordering or not.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
// Process the row move. This means updating the data model to correct the item indices.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath
toIndexPath:(NSIndexPath *)toIndexPath
{
NSString *item = [[appDelegate.categories objectAtIndex:fromIndexPath.row] retain];
[appDelegate.categories removeObject:item];
[appDelegate.categories insertObject:item atIndex:toIndexPath.row];
[item release];
}
Thanks in advance.

in ios 6.1 i was not able to use the above code. I've seen several answers that suggests subclassing the cell class and providing a custom -willTransitionToState. Changing the subview/buttonview frame does not affectively change it's displayed position.
However if you override the layoutSubviews method it will work. Here is the code:
//below i moved the delete button to the left by 10 points. This is because my custom tableview has lef
- (void)layoutSubviews
{
[super layoutSubviews];
for(UIView *subview in self.subviews)
{
if([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellDeleteConfirmationControl"])
{
CGPoint o = subview.center;
subview.center = CGPointMake(o.x - 10, o.y);
}
}
}

You can use your own view by setting the cell's editingAccessoryView property. This can have whatever frame you like, though you'll need to supply a gradient if you want it to look like the standard delete button.
In the xib file of your custom cell, add a new button of whatever size you like (as long as it is smaller than the cell!). This button should not be a subview of your cell, but a separate item in the file.
Connect this button to the editingAccessoryView outlet of your cell and it will replace the standard delete button.

Related

iOS 6: UITableView Edit and Autoresizing

Hi Could someone please help me to understand how to layout table cell's components automatically while editing in iOS 6.0? I have set AutoLayout FALSE for UITableViewCell Autosizing option set to top right in Attributes Inspector! Cell's right side imageviews are overlapping by delete buttons. Please refer attached image. Following is the code for this. Is there anyway I can fix this issue?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PlayerCell *cell = (PlayerCell *)[tableView dequeueReusableCellWithIdentifier:#"PlayerCell"];
Player *player = [self.players objectAtIndex:indexPath.row];
cell.nameLabel.text = player.name;
cell.gameLabel.text = player.game;
cell.ratingImageView.image = [self imageForRating:player.rating];
return cell;
}
- (UIImage *)imageForRating:(int)rating
{
switch (rating)
{
case 1: return [UIImage imageNamed:#"1StarSmall.png"];
case 2: return [UIImage imageNamed:#"2StarsSmall.png"];
case 3: return [UIImage imageNamed:#"3StarsSmall.png"];
case 4: return [UIImage imageNamed:#"4StarsSmall.png"];
case 5: return [UIImage imageNamed:#"5StarsSmall.png"];
}
return nil;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.players removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
I have added following delegate methods and it works fine when I swipe the cell.
-(void)tableView:(UITableView *)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
PlayerCell *cell = (PlayerCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.ratingImageView.hidden = YES;
}
- (void)tableView:(UITableView *)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
PlayerCell *cell = (PlayerCell *)[tableView cellForRowAtIndexPath:indexPath];
cell.ratingImageView.hidden = NO;
}
...but this methods doesn't get invoked when I press editButtonItem Button! That's strange! I am missing something. I have added following method that gets invoked when Edit/Done button pressed but there is no way to detect the cell that will be selected for editing!
- (void)setEditing:(BOOL)editing animated:(BOOL)animate
{
[super setEditing:editing animated:animate];
if(editing)
{
NSLog(#"editMode on");
}
else
{
NSLog(#"Done leave editmode");
}
}
When user clicks left round button, is there anyway to add selector on that button click and get the cell index?
Your ratingImageView uses autoresizing masks to resize and position itself when you are not using AutoLayout. By default this means that the distance to the left and top is fixed and the size won't change.
When you toggle a cell to edit the contentView moved and resizes but your ratingImageView stays fixed to the top and left. You can visualize this by temporarily (for learning purposes) set a background color to the cells contentView and see how it resizes as you edit and delete the cell.
What you want is for your rating view to stay a fixed distance from the right edge instead of the left. You can either change this in InterfaceBuilder (where you do your XIBs or Storyboard) or in code by setting the autoresizingMask property of the ratingImageView.
Go to this tab
Change the autosizing like this
Or do it in code
// in code you specify what should be flexible instead of what should be fixed...
[ratingImageView setAutoresizingMask:UIViewAutoresizingFlexibleLeftMargin];

how to begin moving cell with long press gesture?

I have a UITableView with several UITableViewCells in it. I'd like to long press on a cell to begin move it and keep holding on it and drag to move it. But i don't know how.
I added a long press gesture on table view, when user long pressed , set tableview's editing to YES:
UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:#selector(handleTableViewLongPress:)];
[_tableView addGestureRecognizer:longPressRecognizer];
- (void)handleTableViewLongPress:(UILongPressGestureRecognizer *)gesture
{
if (gesture.state != UIGestureRecognizerStateBegan)
return;
[_tableView setEditing:YES];
}
And the tableview could move cell with two methods:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
//update datasource
}
But with these code user must use two touch to move a cell: one is to set the tableview to editing state, another is to move cell. What i want is to allow user use one touch to do this.
Any suggestions? thanks!
I have found this UITableView subclass: Move table view

Edit & delete multiple rows in UITableView simultaneously

In my app I need to delete multiple rows in a table, edit the table and get a check box beside the table. When checked then the table cells are deleted. It is like the iPhone message app. How can I do this, please help me.
If I understand your question correctly, you essentially want to mark UITableViewCells in some way (a checkmark); then, when the user taps a master "Delete" button, all marked UITableViewCells are deleted from the UITableView along with their corresponding data source objects.
To implement the checkmark portion, you might consider toggling between UITableViewCellAccessoryCheckmark and UITableViewCellAccessoryNone for the UITableViewCell's accessory property. Handle touches in the following UITableViewController delegate method:
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *c = [tableView cellForRowAtIndexPath:indexPath];
if (c.accessoryType == UITableViewCellAccessoryCheckmark) {
[c setAccessoryType:UITableViewCellAccessoryNone];
}
//else do the opposite
}
You might also look at this post regarding custom UITableViewCells if you're wanting a more complex checkmark.
You can set up a master "Delete" button two ways:
The IB approach
The programmatic approach
In either case, eventually a method must be called when the master "Delete" button is pressed. That method just needs to loop through the UITableViewCells in the UITableView and determined which ones are marked. If marked, delete them. Assuming just one section:
NSMutableArray *cellIndicesToBeDeleted = [[NSMutableArray alloc] init];
for (int i = 0; i < [tableView numberOfRowsInSection:0]; i++) {
NSIndexPath *p = [NSIndexPath indexPathWithIndex:i];
if ([[tableView cellForRowAtIndexPath:p] accessoryType] ==
UITableViewCellAccessoryCheckmark) {
[cellIndicesToBeDeleted addObject:p];
/*
perform deletion on data source
object here with i as the index
for whatever array-like structure
you're using to house the data
objects behind your UITableViewCells
*/
}
}
[tableView deleteRowsAtIndexPaths:cellIndicesToBeDeleted
withRowAnimation:UITableViewRowAnimationLeft];
[cellIndicesToBeDeleted release];
Assuming by "edit" you mean "delete a single UITableViewCell" or "move a single UITableViewCell," you can implement the following methods in the UITableViewController:
- (void)viewDidLoad {
[super viewDidLoad];
// This line gives you the Edit button that automatically comes with a UITableView
// You'll need to make sure you are showing the UINavigationBar for this button to appear
// Of course, you could use other buttons/#selectors to handle this too
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
//perform similar delete action as above but for one cell
}
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
//handle movement of UITableViewCells here
//UITableView cells don't just swap places; one moves directly to an index, others shift by 1 position.
}
You can put 1 UIButton lets call it "EDIT" and wire up it to IBAction. In IBAction write so you will be able to do as per your requirement.
-(IBAction)editTableForDeletingRow
{
[yourUITableViewNmae setEditing:editing animated:YES];
}
This will add round red buttons on the left hand corner and you can click on that Delete button will appear click on that and row will be deleted.
You can implement delegate method of UITableView as following.
-(UITableViewCellEditingStyle)tableView: (UITableView *)tableView editingStyleForRowAtIndexPath: (NSIndexPath *)indexPath
{
//Do needed stuff here. Like removing values from stored NSMutableArray or UITableView datasource
}
Hope it helps.
you want to be looking for deleteRowsAtIndexPath, with all your code squeezed between [yourTable beginUpdates] & [yourTable endUpdates];

How to show the reorder controls in UITableView with delete buttons

I read the documentation about how to manage deletion and reordering of rows in a UITableView. I created the edit button and I'm able to delete rows. I would like the user to be able to reorder the rows as well. It seems simple, but I can't understand how to tell the cells that they can be moved.
To tell the rows they can be deleted I use the editingStyleForRowAtIndexPath, but how do I tell the cell it can also be moved and where do I set the showsReorderControl? I tried to place in cellForRowAtIndexPath, but nothing is shown.
Thanks!
You have to say that rows can be moved:
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
and implement this delegate to update your data source:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
See Managing the Reordering of Rows of Table View Programming Guide for iOS
In my case I have implemented all the required UITableViewDelegate methods as mentioned in the Apple document and in the answers here, but still cannot see the reorder control. Eventually I found out it's because I overrode the layoutSubviews method without calling the super's default implementation. After I added the [super layoutSubviews], my reorder control finally appears.
The reason why we need to call [super layoutSubviews] is because when we toggle the table's editing property it would call the cell's layoutSubviews method, and the system provided controls such as the reorder control is displayed within UITableViewCell's default layoutSubviews method. Once you realize this you can also modify your layoutSubviews implementation to change the appearance of your cell depending on whether it is being edited or not to make it less clumsy when the reorder control appears.
So here is a checklist for the row reordering:
make sure the delegate methods tableView:canMoveRowAtIndexPath and tableView:moveRowAtIndexPath:toIndexPath are implemented
make sure the tableView's editing property is set to YES
If you have a custom UITableViewCell, make sure you call
[super layoutSubviews] if you override this method
Adding to #benoit answer above. If your model happens to be a mutable array, something like this would suffice for the tableView:moveRowAtIndexPath:toIndexPath:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
id objectToMove = [_objects objectAtIndex:fromIndexPath.row];
[_objects removeObjectAtIndex:fromIndexPath.row];
[_objects insertObject:objectToMove atIndex:toIndexPath.row];
[tableView reloadData];
[self saveObjects]; // A method of your own to make new positions persistent
}
try this . . .this will handle arranging and updating of cell in case of simple tableview
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
[tableData insertObject: [tableData objectAtIndex:sourceIndexPath.row] atIndex:destinationIndexPath.row];
[tableData removeObjectAtIndex:(sourceIndexPath.row + 1)];
}

Button added to custom section header view disappears when row is deleted

Just came across some very strange behavior in my app. I've recreated the problem in a simplest-case scenario:
NSMutableArray *data;
- (void)viewDidLoad {
[super viewDidLoad];
data = [[NSMutableArray arrayWithObjects:#"1", #"2", #"3", nil] retain];
}
- (UIView *)tableView:(UITableView *)aTableView viewForHeaderInSection:(NSInteger)section {
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 32.0)];
header.backgroundColor = [UIColor lightGrayColor];
[header addSubview:self.button];
return header;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[data removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationTop];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
cell.textLabel.text = [data objectAtIndex:indexPath.row];
return cell;
}
- (void)dealloc {
[super dealloc];
}
Every time I delete a row; the button in my header disappears! This happens no matter what type of rowAnimation I use. If I scroll the table up so that the header scrolls off; the button returns when the header returns. The button is created in the xib file.
I can work around it in one of 2 ways:
Reloading the tableView data after the delete; with a delay so that the deletion animation completes first.
Creating the button in viewForHeaderInSection instead of in the interfaceBuilder.
I'd really like to understand what's going on here. Where is the button going? I've confirmed that viewForHeaderInSection is called when I delete a row.
Edit I tried changing it so that the button is created in viewForHeader, instead of in the xib, but it's causing other strange issues... when I create or delete the button, I am setting certain properties such as the title and enabled depending on how many items there are in the table. When I delete the last row in the table, I don't see the update in text and enabled status until I scroll the button off the screen and back on again.
Because you only have one instance of your button, if the table view decides to create a new header view then the button will be removed from its current parent and moved to the new one. Even if you only have one section in your table, the table view may be doing some strange things internally and recreating header views off-screen so you can't rely on just one being in existence at any one time.
You should create the button in viewForHeaderInSection: and work around your other problems. Rather than only updating the button properties in viewForHeaderInSection you should handle any delete events so that deleting a row will also update the button.
Where is your implementation of the delegate method tableView:heightForHeaderInSection: ? That is necessary for tableView:viewForHeaderInSection: to work correctly. Check the docs.
Reference for UITableView delegate
I've confirmed that
viewForHeaderInSection is called when
I delete a row.
Have you confirmed that viewForHeaderInSection is called for the particular header with the added button?
Then, try adding
[header bringSubviewToFront:self.button];
after adding the button.
Well I at least managed to get around my issue... I made an iVar and property for the view that I create in viewForheaderAtSection, and then I only create a new view if I don't have one already. Otherwise I just return the header I already had; something like this:
- (UIView *)tableView:(UITableView *)aTableView viewForHeaderInSection:(NSInteger)section {
if (!self.myHeader){
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, 32.0)];
header.backgroundColor = [UIColor lightGrayColor];
[header addSubview:self.button];
self.myHeader = header;
[header release];
}
return self.myHeader;
}
This works, but it would still be great to understand what exactly is going on. As far as I can tell, viewForHeaderInSection is being called by the system, but then the instance of the view that I return in that method is not actually being used / shown; at least not until I do something that causes the view to redraw...