在自定义UITableViewCell隐藏/取消隐藏按钮上的UILabel单击

时间:2016-01-23 05:34:25

标签: ios objective-c uitableview

我有一个带有按钮和标签的自定义表格视图单元格。 标签值是从NSMutableArray获取的,默认情况下标签是隐藏的。在按钮单击时,我想取消隐藏相同单元格的标签。在此先感谢。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
selCell = [tableView dequeueReusableCellWithIdentifier:@"beautyIdentifier"];
if (selCell == nil) {

    selCell = [[MDTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"beautyIdentifier"] ;
}
selCell.backgroundColor=[UIColor colorWithRed:0.878 green:0.878 blue:0.878 alpha:1];

    NSObject *labelData=[[reqSubcat objectAtIndex:indexPath.row] objectForKey:@"subcategoryName"];
    NSString *label=[NSString stringWithFormat:@"%@",labelData];

    //Check Button
    checkButton=[[UIButton alloc] init];
    [checkButton setFrame:CGRectMake(20,25,30, 30)];
    checkButton.layer.cornerRadius = checkButton.frame.size.width /2;
    checkButton.clipsToBounds = YES;
    checkButton.layer.borderColor = [UIColor lightGrayColor].CGColor;
    checkButton.layer.borderWidth = 1.f;
    [checkButton addTarget:self action:@selector(checkBox:) forControlEvents:UIControlEventTouchUpInside];
    [checkButton setTitle:label forState:UIControlStateNormal];
    [checkButton setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
    checkButton.tag=indexPath.row;
    [selCell addSubview:checkButton];

NSString *cost=[NSString stringWithFormat:@"₹ %@",[costArray     objectAtIndex:indexPath.row]];
        CGSize stringsize1 = [cost sizeWithAttributes:@{NSFontAttributeName:
                                                            [UIFont systemFontOfSize:40.0f]}];
        costLabel=[[UILabel alloc] init];
        [costLabel setText:cost];
        [costLabel setFrame:CGRectMake(250,20,stringsize1.width, 40)];
        costLabel.font=[UIFont fontWithName:@"Futura" size:25];
        costLabel.textColor=[UIColor colorWithRed:0.153 green:0.239 blue:0.294 alpha:1];
        costLabel.tag=indexPath.row;
        costLabel.hidden=NO;
        [selCell.contentView addSubview:costLabel];
}

- (void)checkBox:(UIButton *)sender
{
   // resetTitle=@"";

NSLog(@"%@",resetTitle);
NSLog(@"%ld",(long)sender.tag);


if ([sender.titleLabel.text isEqualToString:@"✓"])
{

    sender.backgroundColor=[UIColor clearColor] ;
    [sender setTitle:resetTitle forState:UIControlStateNormal];
    [sender setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
    sender.layer.borderColor = [UIColor lightGrayColor].CGColor;


}
else
{
     resetTitle=sender.titleLabel.text;
    NSLog(@"%@",resetTitle);

    sender.backgroundColor=[UIColor colorWithRed:1 green:0.839 blue:0.314 alpha:1] ;
    [sender setTitle:@"✓" forState:UIControlStateNormal];
    [sender.titleLabel setFont:[UIFont fontWithName:@"Futura" size:25]];
    [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    sender.layer.borderColor = [UIColor clearColor].CGColor;

    }

}

1 个答案:

答案 0 :(得分:0)

首先,您不应该向

中的单元格添加任何视图

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

而是在单元格的init方法中添加视图。 (在您的情况下为MDTableViewCell)

其次,按钮的目标应该在单元格中设置,当它被点击时,您将被重定向到单元格,您可以在其中隐藏单元格的标签。