Ios 静态单元格-使用HeightForRowatineXpath方法隐藏

Ios 静态单元格-使用HeightForRowatineXpath方法隐藏,ios,objective-c,sections,Ios,Objective C,Sections,嗨,伙计,我正试图隐藏第1部分(第二部分)中的一些行,具体取决于用户选择的反馈类型: 我使用的是静态单元格,但当我在TypeVC中选择一个选项时,没有任何内容被删除。目前没有错误,但我猜这与我在switch语句中使用的逻辑运算符有关。很抱歉我的代码被丢弃了,但由于我对IOS非常陌生,我不知道你们到底需要看什么 [1] if (variable == (1|2|3)){} 我已经习惯了Java,我经常使用这种语句,因为它可以节省编写时间。在objective-c中是这样做的吗 [2] if (

嗨,伙计,我正试图隐藏第1部分(第二部分)中的一些行,具体取决于用户选择的反馈类型:

我使用的是静态单元格,但当我在TypeVC中选择一个选项时,没有任何内容被删除。目前没有错误,但我猜这与我在switch语句中使用的逻辑运算符有关。很抱歉我的代码被丢弃了,但由于我对IOS非常陌生,我不知道你们到底需要看什么

[1]

if (variable == (1|2|3)){} 
我已经习惯了Java,我经常使用这种语句,因为它可以节省编写时间。在objective-c中是这样做的吗

[2]

if (variable == (1|2|3)){} 
我在哪里出了问题?我是如何让细胞消失的

反馈表视图控制器:

#import "FeedbackTableViewController.h"
#import "TypeTableViewController.h"

@interface FeedbackTableViewController ()

@property NSInteger index;

@end

@implementation FeedbackTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

}

- (void)viewDidAppear:(BOOL)animated{

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (NSIndexPath *) tableView:(UITableView *)tableView
   willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Type: %i",_type);
    if (indexPath.section == 0 && indexPath.row == 0)
        [self performSegueWithIdentifier:@"showTypeVC" sender:self];

    return indexPath;
}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    TypeTableViewController *tvc = [segue destinationViewController];
    tvc.indexchoosen = _index;
}

//- (UITableViewCell *)tableView:(UITableView *)tableView
//         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
//    
//    if (indexPath.row==0) cell.textLabel.text = _typeString;
//    else if (indexPath.row) 
//    
//    return cell;
//}

- (CGFloat)   tableView:(UITableView *)tableView
    heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"section: %i row:%i",indexPath.section, indexPath.row);
    if (indexPath.section == 1) {
        switch (_type) {
            case 0:
                if (indexPath.row==(2|3|4))return 0;
                break;
            case 1:
                if (indexPath.row==(0|1|4))return 0;
                break;
            case 2:
                if (indexPath.row==(0|1|2|3))return 0;
                break;
            case 3:
                return 0;
                break;
            case 4:
                return 0;
                break;
            case 5:
                return 0;
                break;
            default:
                return 0;
                break;
        }
    }
    return 43;
}

- (IBAction)unwindtypeVC:(UIStoryboardSegue *)segue { }


@end
#import "TypeTableViewController.h"
#import "FeedbackTableViewController.h"

@interface TypeTableViewController ()

@end

@implementation TypeTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _typeoptions = @[@"Routing Issues",
                     @"Wrongly Labelled Location",
                     @"Missing Location",
                     @"Comment on Useability",
                     @"Suggestions",
                     @"General Feedback"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}


- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = self.typeoptions[indexPath.row];


    return cell;
}

- (NSIndexPath *)tableView:(UITableView *)tableView
  willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _indexchoosen = indexPath.row;

    return indexPath;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    NSString *string = _typeoptions[_indexchoosen];
    FeedbackTableViewController *fvc1 = [segue destinationViewController];
    fvc1.typeString.text = _typeoptions[_indexchoosen];
    fvc1.type = _indexchoosen;
}

@end
- (CGFloat)   tableView:(UITableView *)tableView
    heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger i = indexPath.row;
    if (indexPath.section == 1) {
        switch (_type) {
            case 0:
                _startLocation.placeholder = @"Start Location:";
                _destination.placeholder = @"Destination:";
                _locationName.placeholder = @"";
                _correctName.placeholder = @"";
                _missingLocation.placeholder = @"";
                if (i==2||i==3||i==4) return 0;
                break;
            case 1:
                _startLocation.placeholder = @"";
                _destination.placeholder = @"";
                _locationName.placeholder = @"Location Name:";
                _correctName.placeholder = @"Correct Name:";
                _missingLocation.placeholder = @"";
                if (i==0||i==1||i==4)return 0;
                break;
            case 2:
                _startLocation.placeholder = @"";
                _destination.placeholder = @"";
                _locationName.placeholder = @"";
                _correctName.placeholder = @"";
                _missingLocation.placeholder = @"Missing Location:";
                if (i==0||i==1||i==2||i==3)return 0;
                break;
            case 3:
                return 0;
                break;
            case 4:
                return 0;
                break;
            case 5:
                return 0;
                break;
            default:
                _startLocation.placeholder = @"";
                _destination.placeholder = @"";
                _locationName.placeholder = @"";
                _correctName.placeholder = @"";
                _missingLocation.placeholder = @"";
                if (i==0||i==1||i==2||i==3||i==4)return 0;
                break;
        }
    } else if (indexPath.section==2 && indexPath.row==2) return 240;
    else if (indexPath.section==0 && indexPath.row==0) return 40;


    return 30;
}
TypeTableViewController:

#import "FeedbackTableViewController.h"
#import "TypeTableViewController.h"

@interface FeedbackTableViewController ()

@property NSInteger index;

@end

@implementation FeedbackTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

}

- (void)viewDidAppear:(BOOL)animated{

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (NSIndexPath *) tableView:(UITableView *)tableView
   willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Type: %i",_type);
    if (indexPath.section == 0 && indexPath.row == 0)
        [self performSegueWithIdentifier:@"showTypeVC" sender:self];

    return indexPath;
}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    TypeTableViewController *tvc = [segue destinationViewController];
    tvc.indexchoosen = _index;
}

//- (UITableViewCell *)tableView:(UITableView *)tableView
//         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
//    
//    if (indexPath.row==0) cell.textLabel.text = _typeString;
//    else if (indexPath.row) 
//    
//    return cell;
//}

- (CGFloat)   tableView:(UITableView *)tableView
    heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"section: %i row:%i",indexPath.section, indexPath.row);
    if (indexPath.section == 1) {
        switch (_type) {
            case 0:
                if (indexPath.row==(2|3|4))return 0;
                break;
            case 1:
                if (indexPath.row==(0|1|4))return 0;
                break;
            case 2:
                if (indexPath.row==(0|1|2|3))return 0;
                break;
            case 3:
                return 0;
                break;
            case 4:
                return 0;
                break;
            case 5:
                return 0;
                break;
            default:
                return 0;
                break;
        }
    }
    return 43;
}

- (IBAction)unwindtypeVC:(UIStoryboardSegue *)segue { }


@end
#import "TypeTableViewController.h"
#import "FeedbackTableViewController.h"

@interface TypeTableViewController ()

@end

@implementation TypeTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _typeoptions = @[@"Routing Issues",
                     @"Wrongly Labelled Location",
                     @"Missing Location",
                     @"Comment on Useability",
                     @"Suggestions",
                     @"General Feedback"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}


- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = self.typeoptions[indexPath.row];


    return cell;
}

- (NSIndexPath *)tableView:(UITableView *)tableView
  willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _indexchoosen = indexPath.row;

    return indexPath;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    NSString *string = _typeoptions[_indexchoosen];
    FeedbackTableViewController *fvc1 = [segue destinationViewController];
    fvc1.typeString.text = _typeoptions[_indexchoosen];
    fvc1.type = _indexchoosen;
}

@end
- (CGFloat)   tableView:(UITableView *)tableView
    heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger i = indexPath.row;
    if (indexPath.section == 1) {
        switch (_type) {
            case 0:
                _startLocation.placeholder = @"Start Location:";
                _destination.placeholder = @"Destination:";
                _locationName.placeholder = @"";
                _correctName.placeholder = @"";
                _missingLocation.placeholder = @"";
                if (i==2||i==3||i==4) return 0;
                break;
            case 1:
                _startLocation.placeholder = @"";
                _destination.placeholder = @"";
                _locationName.placeholder = @"Location Name:";
                _correctName.placeholder = @"Correct Name:";
                _missingLocation.placeholder = @"";
                if (i==0||i==1||i==4)return 0;
                break;
            case 2:
                _startLocation.placeholder = @"";
                _destination.placeholder = @"";
                _locationName.placeholder = @"";
                _correctName.placeholder = @"";
                _missingLocation.placeholder = @"Missing Location:";
                if (i==0||i==1||i==2||i==3)return 0;
                break;
            case 3:
                return 0;
                break;
            case 4:
                return 0;
                break;
            case 5:
                return 0;
                break;
            default:
                _startLocation.placeholder = @"";
                _destination.placeholder = @"";
                _locationName.placeholder = @"";
                _correctName.placeholder = @"";
                _missingLocation.placeholder = @"";
                if (i==0||i==1||i==2||i==3||i==4)return 0;
                break;
        }
    } else if (indexPath.section==2 && indexPath.row==2) return 240;
    else if (indexPath.section==0 && indexPath.row==0) return 40;


    return 30;
}

我希望能有更好的想法来实现我想要达到的目标,所以如果你愿意告诉我一个更有效的方法,我将非常感激。我知道代表可能是一个选择,但我对他们还没有信心,我认为这对我来说会更容易

对于[1],请尝试以下内容并亲自查看:

int test = 3;
if(test == (1 | 2))
    NSLog(@"_MEH_");
因为它是位OR运算,0010 | 0001等于0011,也就是3。因此,我不建议您使用这样的操作。(当然,如果这不是故意的)

对于[2],您应该使用DeleteRowsAndExpaths:withRowAnimation:call For UITableView来删除行

比如,

[self.tableView beginUpdates];

NSIndexPath* rowToDelete = [NSIndexPath indexPathForRow:0 inSection:0];  // For showing purposes only.
NSArray* indexArray = [NSArray arrayWithObjects:rowToDelete, nil];

[self.tableView deleteRowsAtIndexPaths:indexArray withRowAnimation:UITableViewRowAnimationMiddle];

[self.tableView endUpdates];
另外,不要忘记相应地更新数据源。您可能需要从中删除一些对象

self.typeoptions
数组


另外,您还应该更改tableView:numberOfRowsInSection:因为将有少于6行。

我实际上使用了将行高度更改为0的方法

为了有效地执行此操作,我必须删除占位符/行中我不希望显示的任何初始文本。这需要一些情节提要连接,您将看到这些连接被命名为_feedbackText _startLocation等。当用户选择新行时,他们将执行到原始反馈表单的转换,因此调用viewDidAppear。我用它来调用[self.tableView reloadData]。最初,变量_类型的更改实际上不会更改任何内容,但会在重新加载数据时调用HeightForRowatineXpath

我确信在indexPath中使用delete行也会起作用,但我想存储用户在更改反馈类型之前可能键入的信息

新方法:

#import "FeedbackTableViewController.h"
#import "TypeTableViewController.h"

@interface FeedbackTableViewController ()

@property NSInteger index;

@end

@implementation FeedbackTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

}

- (void)viewDidAppear:(BOOL)animated{

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (NSIndexPath *) tableView:(UITableView *)tableView
   willSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"Type: %i",_type);
    if (indexPath.section == 0 && indexPath.row == 0)
        [self performSegueWithIdentifier:@"showTypeVC" sender:self];

    return indexPath;
}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    TypeTableViewController *tvc = [segue destinationViewController];
    tvc.indexchoosen = _index;
}

//- (UITableViewCell *)tableView:(UITableView *)tableView
//         cellForRowAtIndexPath:(NSIndexPath *)indexPath {
//    
//    UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
//    
//    if (indexPath.row==0) cell.textLabel.text = _typeString;
//    else if (indexPath.row) 
//    
//    return cell;
//}

- (CGFloat)   tableView:(UITableView *)tableView
    heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"section: %i row:%i",indexPath.section, indexPath.row);
    if (indexPath.section == 1) {
        switch (_type) {
            case 0:
                if (indexPath.row==(2|3|4))return 0;
                break;
            case 1:
                if (indexPath.row==(0|1|4))return 0;
                break;
            case 2:
                if (indexPath.row==(0|1|2|3))return 0;
                break;
            case 3:
                return 0;
                break;
            case 4:
                return 0;
                break;
            case 5:
                return 0;
                break;
            default:
                return 0;
                break;
        }
    }
    return 43;
}

- (IBAction)unwindtypeVC:(UIStoryboardSegue *)segue { }


@end
#import "TypeTableViewController.h"
#import "FeedbackTableViewController.h"

@interface TypeTableViewController ()

@end

@implementation TypeTableViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _typeoptions = @[@"Routing Issues",
                     @"Wrongly Labelled Location",
                     @"Missing Location",
                     @"Comment on Useability",
                     @"Suggestions",
                     @"General Feedback"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}


- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.textLabel.text = self.typeoptions[indexPath.row];


    return cell;
}

- (NSIndexPath *)tableView:(UITableView *)tableView
  willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    _indexchoosen = indexPath.row;

    return indexPath;
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    NSString *string = _typeoptions[_indexchoosen];
    FeedbackTableViewController *fvc1 = [segue destinationViewController];
    fvc1.typeString.text = _typeoptions[_indexchoosen];
    fvc1.type = _indexchoosen;
}

@end
- (CGFloat)   tableView:(UITableView *)tableView
    heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger i = indexPath.row;
    if (indexPath.section == 1) {
        switch (_type) {
            case 0:
                _startLocation.placeholder = @"Start Location:";
                _destination.placeholder = @"Destination:";
                _locationName.placeholder = @"";
                _correctName.placeholder = @"";
                _missingLocation.placeholder = @"";
                if (i==2||i==3||i==4) return 0;
                break;
            case 1:
                _startLocation.placeholder = @"";
                _destination.placeholder = @"";
                _locationName.placeholder = @"Location Name:";
                _correctName.placeholder = @"Correct Name:";
                _missingLocation.placeholder = @"";
                if (i==0||i==1||i==4)return 0;
                break;
            case 2:
                _startLocation.placeholder = @"";
                _destination.placeholder = @"";
                _locationName.placeholder = @"";
                _correctName.placeholder = @"";
                _missingLocation.placeholder = @"Missing Location:";
                if (i==0||i==1||i==2||i==3)return 0;
                break;
            case 3:
                return 0;
                break;
            case 4:
                return 0;
                break;
            case 5:
                return 0;
                break;
            default:
                _startLocation.placeholder = @"";
                _destination.placeholder = @"";
                _locationName.placeholder = @"";
                _correctName.placeholder = @"";
                _missingLocation.placeholder = @"";
                if (i==0||i==1||i==2||i==3||i==4)return 0;
                break;
        }
    } else if (indexPath.section==2 && indexPath.row==2) return 240;
    else if (indexPath.section==0 && indexPath.row==0) return 40;


    return 30;
}
这将本质上隐藏但不会删除文本字段中的信息。如果您想保留用户输入的任何信息,这非常有用


我希望这能帮助任何试图在分组静态表视图控制器中隐藏行的人

谢谢,当一行要删除时,我应该把它放在哪里。例如,您可以将其放在tableView:didSelectRow…:或按钮操作中。确定谢谢:)如何删除整个部分?另外,如果我删除这些行,它们还能被再次访问吗?我没有使用+1节,但我认为无论何时调用deleteRow方法,都应该调用两次,一次用于节0,一次用于节1。您只是从表中删除索引,如果添加这些索引(并相应地更新源),则可以再次访问它们。