ios 怎么去掉tableview的点击效果

2025-03-23 23:20:29
推荐回答(1个)
回答1:

为了在TableView中使用自定义的UI,所以要取消掉Cell被点击时的蓝色背景。关键代码如下(见红字部分):
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.

CGRect recTableView = CGRectMake(3,102,316,324);

self.tableView = [[UITableView alloc]initWithFrame:recTableViewstyle:UITableViewStylePlain];

// 设置协议,意思就是UITableView类的方法交给了tabView这个对象,让完去完成表格的一些设置操作
self.tableView.delegate = self;
self.tableView.dataSource = self;

//设置TABLE VIEW背景色
self.tableView.backgroundColor = UIColor.clearColor;

//把tabView添加到视图之上
[self.view addSubview:self.tableView];
// 存放显示在单元格上的数据
NSArray *arrayCarList = [NSArrayarrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12", nil];
self.aData = arrayCarList;

[self.tableView setSectionIndexColor:[UIColor clearColor]];
[self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
[self.tableView setSeparatorColor:[UIColor clearColor]];
[self.tableView setBackgroundView:nil];
//[self.tableView setAllowsSelection:NO]; //如果TALBLEVIEW为只读的话也可以直接这样写就行了
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// 声明静态字符串型对象,用来标记重用单元格
static NSString *TableSampleIdentifier = @"BaseCell";

// 用TableSampleIdentifier表示需要重用的单元
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TableSampleIdentifier];

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

cell.contentView.backgroundColor = UIColor.clearColor;

cell.backgroundColor = UIColor.clearColor;

[cell.textLabel setHighlighted:NO];

……

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

[cell setSelectionStyle:UITableViewCellSelectionStyleNone];

........
}