隐藏TableView行而不删除对象数组

时间:2016-01-24 18:49:29

标签: ios objective-c uitableview

我已经搜索并提出了许多不同的方法来完成在表格视图中隐藏行。但似乎没有一个正常工作,它可能是我写它的方式,不确定。我尝试的最后一个例子是调整它的高度,当我运行项目时,所有行都在一行中。

我试图完成的是当登陆此视图控制器并且bool值设置为false时我想隐藏该行但不将其从数组对象中删除,因为当用户按下“发送值”按钮时它将发送即使可能隐藏一行,整个数组的值也是如此。

#import "SensorValueSystem.h"

@interface SensorValueSystem ()

@end


@implementation SensorValueSystem

- (void)viewDidLoad{
    [super viewDidLoad];

    valueArray=[[NSMutableArray alloc]initWithObjects:[NSNumber numberWithInt:0],[NSNumber numberWithInt:0],[NSNumber numberWithInt:0], nil];
    labels = [NSMutableArray arrayWithObjects:@"Temp Sensor", @"Humid Sensor", @"UV Sensor", nil];

}


-(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];

        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10.0f, 10.0f, 140.0f, 21.0f)];
        [cell addSubview:label];
        [label setTag:456];

        UIStepper *stepper = [[UIStepper alloc]initWithFrame:CGRectMake(200.0f, 10.0f, 20.0f, 20.0f)];
        [cell addSubview:stepper];
        [stepper setTag:123];
        [stepper addTarget:self action:@selector(stepperChanged:) forControlEvents:UIControlEventValueChanged];
    }
    [cell setTag:indexPath.row];
    int count = [[valueArray objectAtIndex:indexPath.row] intValue];

    [(UIStepper*)[cell viewWithTag:123] setValue:count];
//    [(UILabel*)[cell viewWithTag:456] setText:[NSString stringWithFormat:@"%@: %d", @"Stepper", count]];
    [(UILabel*)[cell viewWithTag:456] setText:[NSString stringWithFormat:@"%@:   %d", [labels objectAtIndex:indexPath.row], count]];


    return cell;
}


- (void)stepperChanged:(UIStepper*)sender {
    int row = [sender.superview tag];
    int value = (int)[sender value];
    NSLog(@"Stepper Name: %@ at Row: %d = %d",[labels objectAtIndex:row], row,value);

    [valueArray replaceObjectAtIndex:row withObject:[NSNumber numberWithInt:value]];

    [(UILabel*)[(UITableViewCell *)sender.superview viewWithTag:456] setText:[NSString stringWithFormat:@"%@:   %d", [labels objectAtIndex:row], value]];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)component {
    return [labels count];

}

- (IBAction)btnPressed:(id)sender{
    NSLog(@"Values of Array: %@", valueArray);
    NSLog(@"Value of Temp: %@", [valueArray objectAtIndex:0]);
    NSLog(@"Value of Humid: %@", [valueArray objectAtIndex:1]);
    NSLog(@"Value of UV: %@", [valueArray objectAtIndex:2]);

}

@end

标头文件

#import <UIKit/UIKit.h>

@interface SensorValueSystem : UIViewController{
NSMutableArray *valueArray;
    NSMutableArray *labels;
    int passengers;
    int bags;
    int child;
    bool hideTemp;
    bool hideHumid;
    bool hideUV;
}

@property (strong, nonatomic) IBOutlet UITableView *tableView;
- (IBAction)btnPressed:(id)sender;

@end

1 个答案:

答案 0 :(得分:0)

将表数据保存在数组中通常是一件很方便的事情,但这不是必需的。在这种情况下,分离出特殊值并将其保存在自己的指针中可能更简单。您还需要两个簿记变量:布尔值,它告诉您行是否被隐藏,以及特殊行的索引。

@interface TableController ()

@property (strong, nonatomic) SpecialValueClass * specialRowValue;

@end

// This could also be an ivar/property if it needs to change.
static const NSUInteger kSpecialRowIndex = 3;

@implementation TableController
{
    BOOL _specialRowIsShown;
}

然后,在设置了其他值的数组之后,您只需要测试BOOL以了解表视图的行数:

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
    return [valueArray count] + (_specialRowIsShown ? 1 : 0);
}

配置单个单元格是指具有指向特殊值的单独指针将使您的生活更轻松。如果您在特殊行上,则只使用特殊值而不是数组索引算法,否则使用数组:

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Retrieve/create cell...

    NSUInteger row = [indexPath row];
    if( _specialRowIsShown && (row == kSpecialRowIndex) ){
        // Configure cell using data in specialRowValue
    }
    else {
        // Configure cell using data in valueArray[row]
    }

    return cell;
}