IPhone 自定义 UITableViewCell 行缩进 在cell.m中重写setEditing: animated:以使cell进入编辑状态时不右移
转自至:http://www.gowhich.com/blog/133
网上搜索了很多,不知道是我搜索技术问题还是怎么着,就是没有找到,不过经过自己的努力,还是发现了解决的办法,因为这种情况大多数是自定的UITableviewCell,那么肯定知道如何自己的自定的cell里面如何修改cell上面的元素,其实原理很简单,就是重新设置一下元素的frame大小就好了,我讲自己的代码贴到下面希望能够给大家一个指引。如果问题,可以联系我跟我继续探讨。
tableView Cell 进入编辑状态时,让cell不右移动。在cell.m中重写setEditing: animated:方法,以使cell进入编辑状态时不右移attentionListCell.h
//
// attentionListCell.h
// xunYi6
//
// Created by david on 13-5-16.
// Copyright (c) 2013年 david. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface attentionListCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UIImageView *imageViewPic;
@property (strong, nonatomic) IBOutlet UILabel *name;
@property (strong, nonatomic) IBOutlet UILabel *index;
@property (strong, nonatomic) IBOutlet UILabel *rank;
@property (copy, nonatomic) UIImage *listImage;
@property (copy, nonatomic) NSString *listName;
@property (copy, nonatomic) NSString *listIndex;
@property (copy, nonatomic) NSString *listRank;
//--------------------------
//编辑操作
//--------------------------
@property (strong, nonatomic) UIImageView *checkImageView;
@property (nonatomic) BOOL checked;
- (void) setChecked:(BOOL)checked;
@end
attentionListCell.m
//// attentionListCell.m
// xunYi6
//
// Created by david on 13-5-16.
// Copyright (c) 2013年 david. All rights reserved.
//
#import "attentionListCell.h"
@implementation attentionListCell
@synthesize listImage = _listImage;
@synthesize listName = _listName;
@synthesize listIndex = _listIndex;
@synthesize listRank = _listRank;
@synthesize checkImageView = _checkImageView;
@synthesize checked = _checked;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
-(void) setListImage:(UIImage *)value
{
if(![value isEqual:_listImage])
{
_listImage = [value copy];
self.imageViewPic.image = _listImage;
}
}
-(void) setListName:(NSString *)value
{
if(![value isEqualToString:_listName])
{
_listName = [value copy];
self.name.text = _listName;
}
}
-(void) setListIndex:(NSString *)value
{
if(![value isEqualToString:_listIndex])
{
_listIndex = [value copy];
self.index.text = _listIndex;
}
}
-(void) setListRank:(NSString *)value
{
if(![value isEqualToString:_listRank])
{
_listRank = [value copy];
self.rank.text = _listRank;
}
}
- (void) setCheckImageViewCenter:(CGPoint)pt alpha:(CGFloat)alpha animated:(BOOL)animated
{
if (animated)
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
[UIView setAnimationDuration:0.3];
_checkImageView.center = pt;
_checkImageView.alpha = alpha;
[UIView commitAnimations];
}
else
{
_checkImageView.center = pt;
_checkImageView.alpha = alpha;
}
}
//-------------------------------
// 重新定义editing的设置
//-------------------------------
-(void) setEditing:(BOOL)editing animated:(BOOL)animated{
if (self.editing == editing)
{
return;
}
[super setEditing:editing animated:animated];
if (editing)
{
// CGRect *imageFrame = self.imageViewPic.frame;
[self.imageViewPic setFrame:CGRectMake(self.imageViewPic.frame.origin.x + 30, self.imageViewPic.frame.origin.y, self.imageViewPic.frame.size.width, self.imageViewPic.frame.size.height)];
[self.name setFrame:CGRectMake(self.name.frame.origin.x + 30, self.name.frame.origin.y, self.name.frame.size.width, self.name.frame.size.height)];
[self.index setFrame:CGRectMake(self.index.frame.origin.x + 30, self.index.frame.origin.y, self.index.frame.size.width, self.index.frame.size.height)];
[self.rank setFrame:CGRectMake(self.rank.frame.origin.x + 30, self.rank.frame.origin.y, self.rank.frame.size.width, self.rank.frame.size.height)];
// [self.imageViewPic setFrame:CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)]
// self.selectionStyle = UITableViewCellSelectionStyleNone;
// self.backgroundView = [[UIView alloc] init];
// self.backgroundView.backgroundColor = [UIColor redColor];
// self.textLabel.backgroundColor = [UIColor clearColor];
// self.detailTextLabel.backgroundColor = [UIColor clearColor];
//
// if (_checkImageView == nil)
// {
// _checkImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Unselected.png"]];
// [self addSubview:_checkImageView];
// }
//
// [self setChecked:_checked];
// _checkImageView.center = CGPointMake(-CGRectGetWidth(_checkImageView.frame) * 0.5,
// CGRectGetHeight(self.bounds) * 0.5);
// _checkImageView.alpha = 0.0;
// [self setCheckImageViewCenter:CGPointMake(20.5, CGRectGetHeight(self.bounds) * 0.5)
// alpha:1.0 animated:animated];
}
else
{
// _checked = NO;
// self.selectionStyle = UITableViewCellSelectionStyleBlue;
// self.backgroundView = nil;
//
// if (_checkImageView)
// {
// [self setCheckImageViewCenter:CGPointMake(-CGRectGetWidth(_checkImageView.frame) * 0.5,
// CGRectGetHeight(self.bounds) * 0.5)
// alpha:0.0
// animated:animated];
// }
}
}
- (void) setChecked:(BOOL)checked
{
if (checked)
{
_checkImageView.image = [UIImage imageNamed:@"Selected.png"];
self.backgroundView.backgroundColor = [UIColor colorWithRed:223.0/255.0 green:230.0/255.0 blue:250.0/255.0 alpha:1.0];
}
else
{
_checkImageView.image = [UIImage imageNamed:@"Unselected.png"];
self.backgroundView.backgroundColor = [UIColor whiteColor];
}
_checked = checked;
}
@end
项目实际源码
#pragma mark 重写 setEditing: animated:方法
-(void) setEditing:(BOOL)editing animated:(BOOL)animated{
if (self.editing== editing)
{
return;
}
[supersetEditing:editinganimated:animated];
CGFloatmoveSpace = 38;
//cell本会右移,这里讲其往左边移动30,抵消编辑模式的右移距离
if (editing) {
[self.cellBGViewsetFrame:CGRectMake(self.cellBGView.frame.origin.x- moveSpace, self.cellBGView.frame.origin.y,self.cellBGView.frame.size.width,self.cellBGView.frame.size.height)];
}
else{
[self.cellBGViewsetFrame:CGRectMake(self.cellBGView.frame.origin.x+ moveSpace, self.cellBGView.frame.origin.y,self.cellBGView.frame.size.width,self.cellBGView.frame.size.height)];
}
}