【问题标题】:Why can't I hide/show a label in didSelectRowAtIndexPath in tableview, iOS, objective c为什么我不能在 tableview、iOS、objective c 的 didSelectRowAtIndexPath 中隐藏/显示标签
【发布时间】:2016-07-05 10:27:14
【问题描述】:

我有一个表格视图,并且我使用了两个自定义单元格。在自定义单元格中,我设置了一个 uilabel 并隐藏。现在,当用户从表格视图中选择一个单元格时,在 didSelectRowAtIndexPath 方法中,我想显示该标签。我尝试了跟随,

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

}

注意:我使用了断点并检查过,它通过正确的语句导航。但没有任何反应

我也试过了,

[cellone.selectedsignLabelone setHidden:NO];

但什么都没有发生。希望你能帮助 this.thanx。

【问题讨论】:

  • 您确认标签存在了吗?他们有一个非零大小?他们有任何文字吗?
  • 是的,只有“✓”号可用。
  • 不要在cellForRowAtIndexPath之外操作视图(表格视图单元格)。在模型中添加属性selected(数据源数组中项的类型),在cellForRow...中处理并重新加载表格视图。
  • 你这里有多少个部分?
  • 多选

标签: ios objective-c uitableview hidden didselectrowatindexpath


【解决方案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

【讨论】:

    【解决方案2】:

    当您使用自定义单元格时,我认为这可能是自动布局问题。 它正在工作,但标签超出了界限。你能检查一下吗?

    【讨论】:

    • 不,我试过showhide当时显示正确,无法隐藏。
    【解决方案3】:

    如果只选择或单击表格视图行,它将显示标签从隐藏状态变为可见状态,并显示标签中的数据。

    #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
    

    【讨论】:

      猜你喜欢
      • 2018-12-21
      • 2016-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      • 1970-01-01
      • 1970-01-01
      • 2018-03-20
      相关资源
      最近更新 更多