对于像这种要达到红色方块随着选中的Cell显示,而没有选中的要求消失的效果;
思路理解:
1.tag的理解(用来判断)
2.刷新表的理解(因为要想使没选中的Cell后的红色方块消失,必须用到在表刷新,在刷新表时会对每一个cell重新赋值,就是利用这个时候使没选中的Cell红方块消失 )。
看代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *ID=@"tableCell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:ID];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
UserMeaage *message=_accountData[indexPath.row];
if (cell == nil)
{
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
UIButton *accountBtn=[UIButton buttonWithType:UIButtonTypeCustom];
accountBtn.frame=CGRectMake(10, 10, 45 , 45);
accountBtn.backgroundColor=[UIColor grayColor];
accountBtn.layer.cornerRadius=10.0f;
//[accountBtn setTitle:@"用户" forState:UIControlStateNormal];
[accountBtn setImage:[UIImage imageNamed:@"home_navigation_left"] forState:UIControlStateNormal];
[accountBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[cell.contentView addSubview:accountBtn ];
UILabel *titleLable=[[UILabel alloc]initWithFrame:CGRectMake(accountBtn.right + 10, 10, SCRE_WIDTH/2, 50)];
titleLable.text=message.userName;
titleLable.tag=2016+ indexPath.row;
[cell.contentView addSubview:titleLable];
UIView *line=[[UIView alloc]initWithFrame:CGRectMake(accountBtn.right +10,70, SCRE_WIDTH-(accountBtn.right +10), 1)];
line.backgroundColor=[UIColor lightGrayColor];
[cell.contentView addSubview:line];
_maskBtn=[UIButton buttonWithType:UIButtonTypeCustom];
_maskBtn.backgroundColor=[UIColor redColor];
_maskBtn.tag=100 + indexPath.row;
[_maskBtn setTitle:[NSString stringWithFormat:@"%ld", (long)indexPath.row ] forState:UIControlStateNormal];
_maskBtn.frame=CGRectMake( SCRE_WIDTH- 40, 20, 30, 30);
[cell.contentView addSubview:_maskBtn];
// NSLog(@"------------%@",_selectString );
if ([message.userName isEqualToString:_selectString]) {
_maskBtn.hidden =NO;
}else{
_maskBtn.hidden =YES;
}
}
UILabel *ll=[cell.contentView viewWithTag:2016 +indexPath.row];
if ([ll.text isEqualToString:_selectString]) {
UIButton *nn=[cell.contentView viewWithTag:100 +indexPath.row];
nn.hidden=NO;
}
else
{
UIButton *nn=[cell.contentView viewWithTag:100 +indexPath.row];
nn.hidden=YES;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];
UserMeaage *usermessage=_accountData[indexPath.row] ;
_selectString=usermessage.userName;/// 附一个字符串来比较判断;
[_tableView reloadData];
}