Not long ago I asked about how to move reorderControl view close to the center of my custom cell, so this reorderControl break custom design. As I can't move this view and my cell always in editing mode - I just draw this reorderControl in right place of my cell background. But now I have two reorderControls ) one from my background in right place and one from iOS. If I set return value of - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath to NO, iOS reorderControl disappear but I can't reorder rows anymore.
How can I remove iOS reorderControl from screen and still be able to reorder my custom UITableViewCell?
ps cell.showsReorderControl=NO; is not working
It's kind of a hack but the following does work
for(UIView* view in self.subviews)
{
if([[[view class] description] isEqualToString:#"UITableViewCellReorderControl"])
{
for(UIImageView* cellGrip in view.subviews)
{
if([cellGrip isKindOfClass:[UIImageView class]])
[cellGrip setImage:nil];
}
}
}
The solution I got it work in iOS7 is to add another method to find reorder control. Since the tableview structure has changed in iOS7 It was a little bit hard to track UITableViewCellReorderControl. It is now in UITableViewCellScrollView.
The solution is like below
- (void)changeReorderControl:(UITableViewCell *)cell subviewCell:(UIView *)subviewCell
{
if([[[subviewCell class] description] isEqualToString:#"UITableViewCellReorderControl"]) {
[subviewCell.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
UIImageView *imageView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"hand"]];
imageView.center = CGPointMake(subviewCell.frame.size.width/2 , subviewCell.frame.size.height/2);
[subviewCell addSubview:imageView];
}
}
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
for(UIView* subviewCell in cell.subviews)
{
if([[[subviewCell class] description] isEqualToString:#"UITableViewCellScrollView"]) {
for(UIView* subSubviewCell in subviewCell.subviews) {
[self changeReorderControl:cell subviewCell:subSubviewCell];
}
}
}
}
Hope you will get the idea.
Related
This is kind of weird! When I run the app on simulator it works fine but on the device it does not remove the tableHeaderView. Here is my code:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if(UITableViewCellEditingStyleDelete == editingStyle)
{
BOOL success = [Service deleteNoteByNoteId:note];
if(success)
{
[self updateTableViewHeader];
}
}
}
-(void) updateTableViewHeader
{
if([self.selectedVegetableGarden.notes count] > 0)
{
self.tableView.tableHeaderView = [self createSharingView];
}
else
{
self.tableView.tableHeaderView = nil;
}
}
The line self.tableView.tableHeaderView = nil gets triggered but it never removes the table header.
I don't think that is the right way to do it. You're nilifying a view that your table view is managing. Why don't you try instead returning either your custom view or nil form UITableViewDelegate's method - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section. I believe that's the right way to do it. You can ask the OS to call that method by executing the following line:
[self.tableView reloadData];
Hope this helps!
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];
I have a UITableView which is placed on scrollView. Everything is working fine, but when I am editing the table and touching small circular delete button, confirmation Delete rectangular button does not appear. The reason for this is very clear. As the table is scrollable horizontally, the confirmation Delete button appears at last of cell which I cannot see in start. And when I scroll horizontally, it disappears.
So, is there any way I can set the origin of that Delete button, so that it appears in the start?
I am not sure about it but you can try below code in your
- (void)tableView:(UITableView *)aTableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
}
}
write this code
for (UIView *subview in self.subviews) {
if ([NSStringFromClass([subview class]) isEqualToString:#"UITableViewCellDeleteConfirmationControl"]) {
UIView *deleteButtonView = (UIView *)[subview.subviews objectAtIndex:0];
CGRect f = deleteButtonView.frame;
f.size.width = xx;
f.size.height = xx;
deleteButtonView.frame = f;
}
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.
I am using custom cell and adding tableview programmetically. I am trying to implement swipe functinality in my application. The tableview datasource method are not called when I try to swipe.
tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain];
tableview.delegate=self;
tableview.dataSource=self;
tableview.backgroundColor=[UIColor clearColor];
tableview.backgroundColor=selectedBackGroundColor;
tableview.separatorColor = [UIColor clearColor];
tableview.editing=YES;
NSLog(#"%f %f",self.view.frame.size.width,self.view.frame.size.height);
[self.view addSubview:tableview];
and this method is calling
-(UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath
{
return UITableViewCellEditingStyleDelete;
}
after that even cell is also not selecting...am i missing any thing code please help me out?
-(void)layoutSubviews
{
[super layoutSubviews];
}
Then Its Working Fine.....Thanks For answering my Question
Did you implement -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath in the datasource.
If you have set the tableview to editing already I doubt that swipe to delete (and selections if allowsSelectionDuringEditing is NO) will work at all. Try removing this line:
tableview.editing=YES;
and see if the swiping is enabled.
use this implementation instead:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete)
{
// Delete an object
}
}