【IOS】UITableView样式的自定义

不少时候,咱们须要自定义UITableView来知足咱们的特殊要求。这时候,关于UITableView和cell的自定义和技巧太多了,就须要不断的总结和概括。
 
1.添加自定义的Cell。
 
这个问题已经涉及过,可是,这里要说的主要是两种方法的比较!
由于,我常常发现有两种方式:
1.xib方式
这种方式,也就是说,为自定义的UITableViewCell类添加一个xib的文件。而且让二者关联。
这时候,写法为:

 

// 返回cellhtml

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{app

    

    static NSString *CellIdentifier = @"MyCell";ide

    // 自定义cell函数

    MyCell *cell = (MyCell *)[tableVie dequeueReusableCellWithIdentifier:CellIdentifier];布局

    if (cell == nil){学习

        // 这种方式,将会查找响应的xib文件,将不会调用initWithStyle方法动画

        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:niloptions:nil];atom

        cell = [array objectAtIndex:0];url

    }spa

这种方式,是读取了xib文件,因此,就直接按照响应的xib中的布局,布局好了,并不会调用相应的initWithStyle方法。
 
 
2.调用initWithStyle方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"MyCell";

    // 自定义cell

    MyCell *cell = (MyCell *)[tableVie dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil){

        // 这种方式,将会调用cell中的initWithStyle方法

        cell = [[[MyCell alloc] initWithStyle:UITableViewCellSelectionStyleGray reuseIdentifier:CellIdentifier] autorelease];

    }

    return cell;

    

}

 
这种方式,会调用相应Cell类的initWithStyle方法。
 
那么,何时,用那种方式呢?
个人理解是:
当,cell比较简单时,能够添加相应的xib文件,进行关联;当cell比较复杂时,就直接用纯代码的方式(不建立相应的xib文件)。
我发现,我仍是喜欢用纯代码的方式来写,由于,扩展性好,尤为当cell元素复杂甚至带有动画效果的时候,用xib反而很难控制,或者根本没法控制。
我建议用纯代码的方式!
 
 
2.设置cell的setAccessoryView属性
 
主要用在:在右边添加一个自定义的按钮,或者子视图。
为setAccessoryView设置一个按钮。
 

cell.accessoryType = UITableViewCellAccessoryNone;

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    

    [button setFrame:CGRectMake(0.0, 0.0, 55, 57)];

    [button setImage:[UIImage imageNamed:@"tap_normal.png"]forState:UIControlStateNormal];

    [button setImage:[UIImage imageNamed:@"tap_highlight.png"]forState:UIControlStateHighlighted];

    [button setTag:indexPath.row];

    [button addTarget:self action:@selector(doClickPlaybillAction:event:) forControlEvents:UIControlEventTouchUpInside];

 

    [button setBackgroundColor:[UIColor clearColor]];

    [cell setAccessoryView:button];

 

    return cell;

 
经过观察属性定义:

@property(nonatomic) UITableViewCellAccessoryType   accessoryType;              

@property(nonatomic,retain) UIView                 *accessoryView;              

@property(nonatomic) UITableViewCellAccessoryType   editingAccessoryType;       

@property(nonatomic,retain) UIView                 *editingAccessoryView; 

可见,accessoryView属性须要的参数为UIView,因此,能够很方便的自定义。固然还有editingAccessoryView,能够进行自定义修改时的UIView。
 
根据用户点击的按钮,找到相应的Cell
 

- (void) performExpand:(id)paramSender{

 

    UITableViewCell *ownerCell = (UITableViewCell*)[paramSender superview];// 得到父视图,即TableViewCell

    if (ownerCell != nil){

 

        NSIndexPath *ownerCellIndexPath = [self.myTableView indexPathForCell:ownerCell];

        NSLog(@"Accessory in index path is tapped. Index path = %@", ownerCellIndexPath);

        

    }

}

 
3.自定义cell选择时的样式。

 

经过,上面一步,咱们为Cell添加了一个自定义的按钮。

也许就会遇到这么一个纠结的状况,当点击UITableViewCell高亮时,其子视图中不应高亮的对象(好比说自定义的那个按钮)也高亮了。

 

好比:

正确方式:咱们须要cell被选中时,按钮不该该也被高亮显示。如:

 

UITableView <wbr>应用(五)UITableView样式的自定义

 

错误方式:可是,cell被选中时,按钮却也高亮显示了。如:

 

UITableView <wbr>应用(五)UITableView样式的自定义

 

要解决该方法,能够这样:
 

 

 

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{

    [super setHighlighted:highlighted animated:animated];

    

    if(highlighted) {

        [(UIButton *)self.accessoryView setHighlighted:NO];

    }

}

 

 

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{

    [super setSelected:selected animated:animated];

    if(selected) {

        [(UIButton *)self.accessoryView setHighlighted:NO];

    }

}

 
这样,问题时解决了,那若是咱们再深一层次,发问一下:
 
为何UITableViewCell被选中时,UITableViewCell中的其余元素也会被高亮显示呢?
 

由于当UITableViewCell为选中状态时,UITableViewCell把selectedBackgroundView看成一个子视图来添加;

selectedBackgroundView被添加在UITableViewCell的backgroundView之上,或者全部其它视图之下。

当调用setSelected: animated:这一方法时,会致使selectedBackgroundView以一个alpha消化的状态来出现和消失。

还应该注意:

UITableViewCell的selectionStyle值为UITableViewCellSelectionStyleNone时,selectedBackgroundView将不起做用。

 
 
 
 
4.为UITableViewCell添加自定义背景
 
有时候,咱们要为UITableViewCell自定义的类的每一个cell添加自定义的背景图片。
有不少方法:
1.在自定义的UITableViewCell类的initWithStyle方法中,添加以下代码:

// 设置背景

        UIImageView *bgImage=[[[UIImageView alloc] initWithFrame:CGRectMake(0, 0,320, 57)] autorelease];

        [bgImage setImage: [UIImage imageNamed:@"table_live_bg.png"]];

        [self setBackgroundView:bgImage];

 
2.使用setBackgroundImageByName方法或者setBackgroundImage方法
 

[self setBackgroundImageByName:@"table_live_bg.png"];

[self setBackgroundImage:[UIImage imageNamed:@"table_live_bg.png"]];

这种方法,要注意的时,设置的图片大小应该与cell大小相同
 
3.设置cell的contentView,用insertSubview方法
 

[self.contentView insertSubview:messageBackgroundViewbelowSubview:self.textLabel];

        

 self.selectionStyle = UITableViewCellSelectionStyleNone;

 
这三种方式,都在initWithStyle方法中设置。
 
5.设置删除Cell时的自定义文本
 

//定制Delete字符串,添加函数 返回要显示的字符串

-(NSString *)tableView:(UITableView*)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{

    return @"删除";

}

 
 本文转载自:http://blog.sina.com.cn/s/blog_7b9d64af0101aaoy.html
仅供学习参考
相关文章
相关标签/搜索