隐藏uitableview的特定视图

时间:2016-04-26 07:18:54

标签: ios objective-c uitableview

我有一张桌子视图。以下是我的cellForRowAtIndexPath代码

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

    UITableViewCell *cell = nil;
    cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@""];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:nil];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    orderDetailsLabel = [[UILabel alloc]init];
    shipmentStatusLabel = [[UILabel alloc]init];
    ratingandFeedbackLabel = [[UILabel alloc]init];

    HCSStarRatingView *starRatingView;

    UILabel *suggestionsLabel;
    UILabel *checkBoxLabel1;
    UIButton *checkBoxButton1;
    UIButton *saveButton;
    UIImageView *checkbox1;
    UITextView *feedBackTextView;

    suggestionsLabel = [[UILabel alloc]init];

    checkBoxLabel1 = [[UILabel alloc]init];


    checkBoxButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
    checkbox1 = [[UIImageView alloc]init];
    feedBackTextView = [[UITextView alloc]init];

    if (indexPath.row == 0)
    {

        ratingandFeedbackLabel.frame = CGRectMake(20, 5, 350, 20);
        ratingandFeedbackLabel.text = [ratingandFeedbackArray objectAtIndex:indexPath.row];
        ratingandFeedbackLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14];

        starRatingView = [[HCSStarRatingView alloc] initWithFrame:CGRectMake(20, 15, 100, 50)];
        starRatingView.maximumValue = 5;
        starRatingView.minimumValue = 0;
        starRatingView.value = 0;
        starRatingView.tintColor = [UIColor colorWithRed:254/255.0 green:174/255.0 blue:77/255.0 alpha:1];
        starRatingView.backgroundColor = [UIColor clearColor];
        [starRatingView addTarget:self action:@selector(didChangeValue:) forControlEvents:UIControlEventValueChanged];

    }
    else if (indexPath.row == 1)
    {
        checkbox1.frame = CGRectMake(20, 0, 20, 20);
        checkbox1.image = [UIImage imageNamed:@"checkbox-nottick-60@2x.png"];
        checkBoxButton1.frame = CGRectMake(20, 0, 20, 20);
        [checkBoxButton1 addTarget:self action:@selector(checkBoxButton1Tapped:) forControlEvents:UIControlEventTouchUpInside];

        suggestionsLabel.frame = CGRectMake(10, 185, 300, 30);
        suggestionsLabel.text = @"Your feedback will help us make better *";
        suggestionsLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:13];

        CGRect screenRect = [[UIScreen mainScreen] bounds];

        if (screenRect.size.height == 480)
        {
            tableView.scrollEnabled = YES;
            feedBackTextView.frame = CGRectMake(18, 220, 285, 100);
            saveButton.frame = CGRectMake(120, 330, 70, 30);

        }

        UIColor *borderColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1.0];
        feedBackTextView.text = @"";

        [saveButton setTitle:@"Save" forState:UIControlStateNormal];
        [saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [saveButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];

    }

    tableView.tableFooterView  = [UIView new];

    [cell.contentView addSubview:orderDetailsLabel];
    [cell.contentView addSubview:shipmentStatusLabel];
    //[cell.contentView addSubview:starRatingImageView];
    [cell.contentView addSubview:ratingandFeedbackLabel];
    [cell.contentView addSubview:suggestionsLabel];

    [cell.contentView addSubview:checkBoxButton1];

    [cell.contentView addSubview:checkBoxLabel1];
    [cell.contentView addSubview:feedBackTextView];
    [cell.contentView addSubview:starRatingView];
    [cell.contentView addSubview:saveButton];

    return cell;
}

在另一个名为receiveStarRatingData的方法中,我从用户那里接收状态码。如果状态代码是1,我不想显示checkbox,suggestionsLabel,save button和feedBackTextView。如何隐藏它们或从方法receiveStarRatingData中删除它们?

1 个答案:

答案 0 :(得分:1)

你可以在tableView控制器中有一个布尔变量,如isStatusCodeOne :)一旦你在receiveStarRatingData方法中接收数据,如果状态码为1,则将此变量设置为true :)并通过调用self.tableView.reloadData()句柄标签重新加载tableView在cellForRowatIndexpath喜欢

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@""];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:nil];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    orderDetailsLabel = [[UILabel alloc]init];
    shipmentStatusLabel = [[UILabel alloc]init];
    ratingandFeedbackLabel = [[UILabel alloc]init];

    HCSStarRatingView *starRatingView;

    UILabel *suggestionsLabel;
    UILabel *checkBoxLabel1;
    UIButton *checkBoxButton1;
    UIButton *saveButton;
    UIImageView *checkbox1;
    UITextView *feedBackTextView;

    if (!self.isStatusCodeOne) {
        suggestionsLabel = [[UILabel alloc]init];

        checkBoxLabel1 = [[UILabel alloc]init];


       checkBoxButton1 = [UIButton buttonWithType:UIButtonTypeCustom];
       checkbox1 = [[UIImageView alloc]init];
       feedBackTextView = [[UITextView alloc]init];
    }

    if (indexPath.row == 0)
    {

        ratingandFeedbackLabel.frame = CGRectMake(20, 5, 350, 20);
        ratingandFeedbackLabel.text = [ratingandFeedbackArray objectAtIndex:indexPath.row];
        ratingandFeedbackLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:14];

        starRatingView = [[HCSStarRatingView alloc] initWithFrame:CGRectMake(20, 15, 100, 50)];
        starRatingView.maximumValue = 5;
        starRatingView.minimumValue = 0;
        starRatingView.value = 0;
        starRatingView.tintColor = [UIColor colorWithRed:254/255.0 green:174/255.0 blue:77/255.0 alpha:1];
        starRatingView.backgroundColor = [UIColor clearColor];
        [starRatingView addTarget:self action:@selector(didChangeValue:) forControlEvents:UIControlEventValueChanged];

    }
    else if (indexPath.row == 1)
    {
        if(!isStatusCodeOne){
            checkbox1.frame = CGRectMake(20, 0, 20, 20);
            checkbox1.image = [UIImage imageNamed:@"checkbox-nottick-60@2x.png"];
            checkBoxButton1.frame = CGRectMake(20, 0, 20, 20);
            [checkBoxButton1 addTarget:self action:@selector(checkBoxButton1Tapped:) forControlEvents:UIControlEventTouchUpInside];

            suggestionsLabel.frame = CGRectMake(10, 185, 300, 30);
            suggestionsLabel.text = @"Your feedback will help us make better *";
            suggestionsLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:13];

        }
        CGRect screenRect = [[UIScreen mainScreen] bounds];

        if (screenRect.size.height == 480)
        {
            tableView.scrollEnabled = YES;
            feedBackTextView.frame = CGRectMake(18, 220, 285, 100);
            saveButton.frame = CGRectMake(120, 330, 70, 30);

        }

        UIColor *borderColor = [UIColor colorWithRed:204.0/255.0 green:204.0/255.0 blue:204.0/255.0 alpha:1.0];
        feedBackTextView.text = @"";

        [saveButton setTitle:@"Save" forState:UIControlStateNormal];
        [saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [saveButton addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];

    }


    }
    tableView.tableFooterView  = [UIView new];
    [cell.contentView addSubview:orderDetailsLabel];
    [cell.contentView addSubview:shipmentStatusLabel];
    //[cell.contentView addSubview:starRatingImageView];
    [cell.contentView addSubview:ratingandFeedbackLabel];
    if(!isStatusCodeOne){
       [cell.contentView addSubview:suggestionsLabel];

       [cell.contentView addSubview:checkBoxButton1];

       [cell.contentView addSubview:checkBoxLabel1];
       [cell.contentView addSubview:feedBackTextView];
       [cell.contentView addSubview:starRatingView];
       [cell.contentView addSubview:saveButton];
    }
    return cell;
}

并在设置isStatusCodeOne之后在receiveStarRatingData方法中调用self.tableView.reloadData():)