如何将UITableViewCellSelectionStyle属性设置为某种自定义颜色? [英] How do I set UITableViewCellSelectionStyle property to some custom color?

查看:483
本文介绍了如何将UITableViewCellSelectionStyle属性设置为某种自定义颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在开发一个iPhone应用程序,在我的表视图中我想要自定义颜色的单元格选择风格,我阅读 UITableViewCell类参考,但只有三个常数定义选择风格(蓝色,灰色, 没有)。我看到一个应用程序使用不同于参考中定义的颜色。



我们如何使用参考中定义的颜色之外的颜色?



提前感谢。

解决方案

选择是在构建单元格时设置单元格上的 selectedBackgroundView



ie

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

static NSString * CellIdentifier = @细胞;
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil){
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@SelectedCellBackground.png]]] autorelease];
}

//配置单元格
}

使用的图像应该有一个漂亮的渐变(像默认选择)。如果你只是想要一个平面颜色,你可以使用UIView,而不是 UIImageView ,并将 backgroundColor 设置为颜色



当选择此行时,系统会自动应用此背景。


I am developing an iPhone application, in my table view I wanted custom color for Cell Selection Style, I read the UITableViewCell Class Reference but there are only three constants defined for Selection style (Blue, Gray, None). I saw one application that used a different color than those defined in the reference.

How can we use a color other than those defined in the reference?

Thanks in advance.

解决方案

The best way to set the selection is to set the selectedBackgroundView on the cell when you construct it.

i.e.

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"SelectedCellBackground.png"]] autorelease];
    }

    // configure the cell
}

The image used should have a nice gradient (like the default selection). If you just want a flat color, you can use a UIView instead of a UIImageView and set the backgroundColor to the color you want.

This background is then automatically applied when the row is selected.

这篇关于如何将UITableViewCellSelectionStyle属性设置为某种自定义颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆