簡體   English   中英

在自定義UITableViewCell中,在按鈕上隱藏/取消隱藏UILabel單擊

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

我有一個帶有按鈕和標簽的自定義表格視圖單元格。 標簽值是從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;

    }

}

首先,您不應該在單元格內添加任何視圖

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

而是將視圖添加到單元格的init方法中。 (您的情況下為MDTableViewCell)

其次,應將按鈕的目標設置在單元格內,點擊該按鈕后,您將被重定向到該單元格,您可以在其中隱藏該單元格的標簽。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM