2016-07-05 13 views
0

У меня есть вид таблицы, и я использую две пользовательские ячейки. в пользовательской ячейке я установил uilabel и скрыл. Теперь, когда пользователь выбирает ячейку из табличных, в методе didSelectRowAtIndexPath, я хочу показать, что label.I пробовал с последующим,Почему я не могу скрыть/показать метку в файле didSelectRowAtIndexPath в tableview, iOS, объекте c

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    HoteldetalcelloneTableViewCell *cellone = [tableView dequeueReusableCellWithIdentifier:@"cellone"]; 
    HoteldetailcelltwoTableViewCell *celltwo = [tableView dequeueReusableCellWithIdentifier:@"celltwo"]; 


    if(indexPath.section == 0) 
    { 
     HotelDetailsone *secone = [roomonearray objectAtIndex:indexPath.row]; 
     if([secone.offerallow isEqualToString:@"True"]) 
     { 
      celltwo.selectedsignLabel.hidden = NO; 


     } 
     else 
     { 
      cellone.selectedsignLabelone.hidden = NO; 



     } 
     NSLog(@"price for room 1 : %@", secone.itempriceText); 

    } 
    else 
    { 
     HotelDetailsone *sectwo = [roomtwoarray objectAtIndex:indexPath.row]; 
     NSLog(@"price for room 2 : %@", sectwo.itempriceText); 
    } 

} 

ПРИМЕЧАНИЯ: Я использовал точку останова и проверил, что перемещаться по правильно statement.but ничего не случится

Я пытался со следующими также,

[cellone.selectedsignLabelone setHidden:NO]; 

но ничего happen.hope ваша помощь с this.thanx.

+0

Вы подтвердили, что этикетки существуют? Что у них ненулевой размер? Что у них есть текст? – Avi

+0

Да, доступен только знак «✓». –

+1

** Не манипулируйте представлением (ячейкой просмотра таблицы) вне 'cellForRowAtIndexPath' **. Добавьте свойство 'selected' в модель (тип элементов в массиве источников данных), обработайте его в' cellForRow ... 'и перезагрузите представление таблицы. – vadian

ответ

0

Как вы использовали пользовательскую ячейку, я думаю, что это может быть проблема автоспуск. Он работает, но ярлык выходит из строя. Можете ли вы проверить это?

+0

нет, я попробовал 'show' и' hide', в то время он корректно показывает и не может скрыть. –

1

UITableView повторно использует ячейки для сохранения памяти: при вызове dequeueReusableCellWithIdentifier: вы получаете объект ячейки, который будет изменен при следующем просмотре таблицы, чтобы нарисовать ячейку, поэтому при ее изменении она будет быть выброшенным потом.

Объект ячейки, который фактически загружен и показан, возвращается в методе cellForRowAtIndexPath:. Попробуйте изменить ячейку внутри cellForRowAtIndexPath:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow]; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellone"]; 
    if (indexPath == selectedIndexPath) // this is the selected cell 
    { 
     cell.selectedsignLabelone.hidden = NO; 
    } 
    return cell; 
} 

Если вы хотите обновить клетки внутри метода didSelectRowAtIndexPath:, попытайтесь [tableView reloadData] в конце концов и заменить dequeueReusableCellWithIdentifier: с cellForRowAtIndexPath.

+0

это не сработало для меня –

0

Если вы выбираете или выбираете только строку таблицы, она отображает ярлык из скрытого состояния в видимый, а также отображает данные в метке.

#import "ViewController.h" 
#import "CustomCell.h" 


@interface ViewController() 
{ 
    NSMutableArray *arrCustomCellOne,*arrCustomCellTwo,*arraySection; 
} 
@end 
@implementation ViewController 
@synthesize tblviewTwoCustomCell; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    arrCustomCellOne = [[NSMutableArray alloc]initWithObjects:@"Tim Cook",@"Sathya Nadella",@"Mark",@"Sundaram Pichai",nil]; 
    arrCustomCellTwo = [[NSMutableArray alloc]initWithObjects:@"Apple",@"Microsoft",@"Facebook",@"Google",nil]; 
    arraySection = [[NSMutableArray alloc]initWithObjects:@"Name",@"Company",nil]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

#pragma mark - UITableViewDataSource Methods 

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return arraySection.count; 
} 
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{ 
    if(section == 0) 
    { 
    return @"Name"; 
    } 
    else 
    { 
    return @"Company"; 
    } 
} 
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    if(section==0) 
    return arrCustomCellOne.count; 
    else 
    return arrCustomCellTwo.count; 
} 

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:@"cell"]; 
    NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"CustomCell" owner:self options:nil]; 
    if([[arraySection objectAtIndex:indexPath.section]isEqualToString:@"Name"]) 
    { 
     if(cell == nil) 
     cell = nibs[0]; 

     cell.lblName.text = [NSString stringWithFormat:@"%@",[arrCustomCellOne objectAtIndex:indexPath.row]]; 
    } 
    if([[arraySection objectAtIndex:indexPath.section]isEqualToString:@"Company"]) 
    { 
     if(cell == nil) 
      cell = nibs[1]; 

     cell.lblCompany.text = [NSString stringWithFormat:@"%@",[arrCustomCellTwo objectAtIndex:indexPath.row]]; 
    } 
    return cell; 
} 


#pragma mark - UITableViewDlegate Methods 

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    CustomCell *cell = (CustomCell *)[tableView cellForRowAtIndexPath:indexPath]; 
    if([[arraySection objectAtIndex:indexPath.section]isEqualToString:@"Name"]) 
    { 
     cell.lblName.hidden = NO; 
     cell.lblName.text = [NSString stringWithFormat:@"%@",[arrCustomCellOne objectAtIndex:indexPath.row]]; 
    } 
    else 
    { 
     cell.lblCompany.hidden = NO; 
     cell.lblCompany.text = [NSString stringWithFormat:@"%@",[arrCustomCellTwo objectAtIndex:indexPath.row]]; 
    } 
} 

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section 
{ 
    return 35; 
} 

@end