选择表格视图单元格时显示自定义 UIImageView
Posted
技术标签:
【中文标题】选择表格视图单元格时显示自定义 UIImageView【英文标题】:Display custom UIImageView when table view cell is selected 【发布时间】:2014-10-11 06:37:00 【问题描述】:我在表格视图单元格上使用自定义 UIImageView
和 UITextField
。一切正常,但是当我选择任何单元格时,imageview 会被隐藏,但 textfield 会显示。那么问题是什么,我该如何解决呢?
这是我在 tableview 上使用图像和文本字段的代码 -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *cellidentitifier = @"cell";
UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellidentitifier];
if (cell ==nil)
cell = [tableView dequeueReusableCellWithIdentifier:cellidentitifier];
else
[cell prepareForReuse];
UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 68, 70)];
imgview.backgroundColor = [UIColor blackColor];
imgview.userInteractionEnabled = NO;
imgview.layer.cornerRadius =YES;
[cell.contentView addSubview:imgview];
_nameTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 10, 160, 20)];
_nameTF.placeholder =@"Name";
_nameTF.layer.borderWidth = 1.0;
[cell.contentView addSubview:_nameTF];
_nameTF.delegate = self;
_nameTF.userInteractionEnabled = NO;
_nameTF = nil;
_addressTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 34, 160, 20)];
_addressTF.placeholder = @"Address";
_addressTF.layer.borderWidth= 1.0;
[cell.contentView addSubview:_addressTF];
_addressTF.delegate = self;
_addressTF.userInteractionEnabled = NO;
_addressTF = nil;
_PhoneTF = [[UITextField alloc]initWithFrame:CGRectMake(120, 58, 160, 20)];
_PhoneTF.placeholder = @"Phone";
_PhoneTF.layer.borderWidth= 1.0;
[cell.contentView addSubview:_PhoneTF];
_PhoneTF.delegate = self;
_PhoneTF.userInteractionEnabled = NO;
_PhoneTF = nil;
_slotTF = [[UITextField alloc]initWithFrame:CGRectMake(30, 84, 250, 18)];
_slotTF.placeholder = @"Slot";
_slotTF.layer.borderWidth= 1.0;
[cell.contentView addSubview:_slotTF];
_slotTF.delegate = self;
_slotTF.userInteractionEnabled = NO;
_slotTF = nil;
return cell;
【问题讨论】:
【参考方案1】:试试这个
UIImageView *imgview = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 68, 70)];
imgview.backgroundColor = [UIColor blackColor];
imgview.layer.borderWidth = 1.0;
imgview.userInteractionEnabled = NO;
imgview.layer.cornerRadius =YES;
[cell.contentView addSubview:imgview];
然后你选择了一个单元格,你发现imgview还在 因此,我认为选择一个单元格时,此单元格的背景颜色变为gray.so,覆盖了IMGView的BlackColor。它不是隐藏的
【讨论】:
以上是关于选择表格视图单元格时显示自定义 UIImageView的主要内容,如果未能解决你的问题,请参考以下文章