简体   繁体   English

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

[英]In a custom UITableViewCell Hide/Unhide UILabel on Button Click

I have a custom table view cell with a button and a label. 我有一个带有按钮和标签的自定义表格视图单元格。 Label value is fetched from NSMutableArray and label is hidden by default. 标签值是从NSMutableArray获取的,默认情况下标签是隐藏的。 On button click I want to unhide label of the same cell. 在按钮上单击我要取消隐藏同一单元格的标签。 Thanks in Advance. 提前致谢。

- (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;

    }

}

First of all, you shouldn't add any views to cell inside 首先,您不应该在单元格内添加任何视图

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

Instead, add the views in the cell's init method. 而是将视图添加到单元格的init方法中。 (MDTableViewCell in your case) (您的情况下为MDTableViewCell)

Second, the button's target should be set within cell, and when it's tapped you will be redirected to the cell, where you can hide the cell's label. 其次,应将按钮的目标设置在单元格内,点击该按钮后,您将被重定向到该单元格,您可以在其中隐藏该单元格的标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM