2016-01-23 100 views
0

我有一個帶有按鈕和標籤的自定義表格視圖單元格。 標籤值是從NSMutableArray中獲取的,默認情況下標籤是隱藏的。點擊按鈕時,我想取消隱藏同一個單元格的標籤。提前致謝。在自定義UITableViewCell按鈕上隱藏/取消隱藏UILabel點擊

- (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 
{ 
    // [email protected]""; 

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; 

    } 

} 
+0

發佈你的代碼,你試過了什麼? –

回答

0

首先,你應該沒有什麼意見裏面

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath添加到細胞。

而是在單元的init方法中添加視圖。 (MDTableViewCell在你的情況)

其次,按鈕的目標應該設置在單元格內,當它被點擊時,你將被重定向到單元格,在那裏你可以隱藏單元格的標籤。

+0

我正在使用的單元格來自Machine Design Library。此解決方案是否適用於此?我在許多其他視圖控制器上重複使用它們。 – PalviRane

+0

是的。這會工作。你只需要修改''''MDTableViewCell''的''' - (instancetype)initWithStyle:(UITableViewCellStyle)風格'重用標識符:(NSString *)reuseIdentifier',並在那裏添加你的標籤,按鈕和按鈕的目標。 – Varun