繁体   English   中英

如何在ios7 / uitableview中的按钮单击上隐藏行?

[英]How hide a row on button click in ios7/uitableview?

嗨,我是iOS开发的新手。

我有一个UITableViewController ,其中包含4个部分,每个部分一行。
在第1部分中,我有一个Hide按钮,该按钮应该隐藏第1部分中的行。
当我再次单击此按钮时,行应重新出现。

我正在使用以下代码:

-(void)hide:(id)sender
{
    HideBtn = (UIButton *)sender;
    isReadMoreButtonTouched = YES;

    [[self tableView] beginUpdates];
    [[self tableView] reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForItem: HideBtn.tag
                                                                   inSection:0]]
                            withRowAnimation:UITableViewRowAnimationAutomatic];
    [[self tableView] endUpdates];
}

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

    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
    }

    if (indexPath.section==0) {
        if(isReadMoreButtonTouched && [indexPath row] == indexOfReadMoreButton) {
            //design your read more label here
            CGRect detailFrame = [Utility getlabelHeight:CGRectMake(10,0,310,20)
                                            withFontName:appFont
                                             andFontSize:15
                                                 andText:description];
            DeatilsTxtView = [[UILabel alloc] initWithFrame:detailFrame];
            DeatilsTxtView.backgroundColor = [UIColor whiteColor];
            DeatilsTxtView.font = [UIFont fontWithName:appFont size:15];
            DeatilsTxtView.text = description;
            [DeatilsTxtView setLineBreakMode:NSLineBreakByWordWrapping];
            DeatilsTxtView.numberOfLines = 0;
            [DeatilsTxtView sizeToFit];
            [cell.contentView addSubview:DeatilsTxtView];
        }

    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 && isReadMoreButtonTouched && [indexPath row] == indexOfReadMoreButton) {
        float height = 10;
        description = [description stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        CGRect frame1 = [Utility getlabelHeight:CGRectMake(0,10, 250,20)
                                   withFontName:appFont
                                    andFontSize:15
                                        andText:description];
        height = height+frame1.size.height;
        return height;
    }
}

使用此代码,当我运行该时间行不可见时,当我单击“隐藏”按钮行可见时, 再次单击该行时,它不可见。

我想要当我单击“隐藏”按钮时该时间行不可见,而当我再次单击时,该行应变为可见。

这不是躲在你是不是做isReadMoreButtonTouched标志为假 ,也没有提供在cellForRowAtIndex方法FALSE的任何步骤之前。 查看以下要添加的行。

-(void)hide:(id)sender
{
  HideBtn = (UIButton *)sender;

  If(!isReadMoreButtonTouched);
    isReadMoreButtonTouched = YES;
  else
    isReadMoreButtonTouched = N0; /* Making isReadMoreButtonTouched FALSE for making row invisible on second tap of button */

  [[self tableView] beginUpdates];
  [[self tableView] reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForItem: HideBtn.tag  inSection:0]] withRowAnimation:UITableViewRowAnimationAutomatic];
  [[self tableView] endUpdates];
}

同样在cellForRowAtIndex方法中-

if(isReadMoreButtonTouched && [indexPath row]== indexOfReadMoreButton)
{
    // design your read more label here
//Detail TextView
[cell.contentView addSubview:DeatilsTxtView];
}
else     /* ADD THIS ELSE PART SO AS TO REMOVE SUBVIEW ADDED */
{
  [cell.contentView removeFromSuperview];
}

同样,您将根据需要在heightForRowAtIndexPath方法中提供错误条件。

如果有任何疑问,请告诉我。

暂无
暂无

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

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