iphone-67-长按uitableview的row,实现reorder
(2012-06-11 15:10:26) A component of one of our upcoming apps is a Scramble type game where letters are rearranged to solve the word. I had a fantastic idea to use a
Open the project startpoint
NSLog(@"%@", cell.subviews);
Run the project and you will see a view called “UITableViewCellReorderCo
Because we dont have the class definition for “UITableViewCellReorderCo
for(UIView* view in cell.subviews)
{
if([[[view class] description] isEqualToString:@"UITableViewCellReorderCo ntrol"])
{
NSLog(@"%@", view);
[view setBackgroundColor:[UIColor redColor]];
}
}
Run the project, only the “UITableViewCellReorderCo
You can try setting the frame of this view, but the
for(UIView* view in cell.subviews)
{
if([[[view class] description] isEqualToString:@"UITableViewCellReorderCo ntrol"])
{
[view setBackgroundColor:[UIColor redColor]];
UIView* resizedGripView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(view.frame), CGRectGetMaxY(view.frame))];
[resizedGripView setBackgroundColor:[UIColor greenColor]];
[resizedGripView addSubview:view];
[cell addSubview:resizedGripView];
[resizedGripView release];
}
}
Run the app and now all cell’s are filled with our custom green view with the red grip on the right. Reordering functionality will still work. Now it’s time to figure our what transform this custom view requires for it to fill the entire cell.
for(UIView* view in cell.subviews)
{
if([[[view class] description] isEqualToString:@"UITableViewCellReorderCo ntrol"])
{
[view setBackgroundColor:[UIColor redColor]];
UIView* resizedGripView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(view.frame), CGRectGetMaxY(view.frame))];
[resizedGripView setBackgroundColor:[UIColor greenColor]];
[resizedGripView addSubview:view];
[cell addSubview:resizedGripView];
[resizedGripView release];
CGSize sizeDifference = CGSizeMake(resizedGripView.frame.size.width - view.frame.size.width, resizedGripView.frame.size.height - view.frame.size.height);
CGSize transformRatio = CGSizeMake(resizedGripView.frame.size.width / view.frame.size.width, resizedGripView.frame.size.height / view.frame.size.height);
// Original transform
CGAffineTransform transform = CGAffineTransformIdentit y;
// Scale custom view so grip will fill entire cell
transform = CGAffineTransformScale(transform, transformRatio.width, transformRatio.height);
// Move custom view so the grip's top left aligns with the cell's top left
transform = CGAffineTransformTransla te(transform, -sizeDifference.width / 2.0, -sizeDifference.height / 2.0);
[resizedGripView setTransform:transform];
}
}
This will make the grip take up the entire cell, you can now reorder from any point on the cell, perfect. Now just some cleanup, remove the red and green colours from the custom view and the “UITableViewCellReorderCo
for(UIImageView* cellGrip in view.subviews)
{
if([cellGrip isKindOfClass:[UIImageView class]])
[cellGrip setImage:nil];
}
And that is all that’s required, not too much code at all, just a bit of subview hunting and testing to see what works. Here is the complete
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
// Grip customization code goes in here...
for(UIView* view in cell.subviews)
{
if([[[view class] description] isEqualToString:@"UITableViewCellReorderCo ntrol"])
{
UIView* resizedGripView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetMaxX(view.frame), CGRectGetMaxY(view.frame))];
[resizedGripView addSubview:view];
[cell addSubview:resizedGripView];
[resizedGripView release];
CGSize sizeDifference = CGSizeMake(resizedGripView.frame.size.width - view.frame.size.width, resizedGripView.frame.size.height - view.frame.size.height);
CGSize transformRatio = CGSizeMake(resizedGripView.frame.size.width / view.frame.size.width, resizedGripView.frame.size.height / view.frame.size.height);
// Original transform
CGAffineTransform transform = CGAffineTransformIdentit y;
// Scale custom view so grip will fill entire cell
transform = CGAffineTransformScale(transform, transformRatio.width, transformRatio.height);
// Move custom view so the grip's top left aligns with the cell's top left
transform = CGAffineTransformTransla te(transform, -sizeDifference.width / 2.0, -sizeDifference.height / 2.0);
[resizedGripView setTransform:transform];
for(UIImageView* cellGrip in view.subviews)
{
if([cellGrip isKindOfClass:[UIImageView class]])
[cellGrip setImage:nil];
}
}
}
}
And here is the project endpoint.