Question

Hi guy I'm trying to hide some of the rows in section 1 (Second section) depending on what type of feedback the user has selected:

I'm using static cells but at the moment nothing is being removed when I select one of the options in the TypeVC. There are no errors at the moment but having a guess I think its something to do with the logical operators I'm using in the switch statement. Sorry for dumping my code but as I'm very new to IOS I don't know what exactly you guys would need to see.

[1]

if (variable == (1|2|3)){} 

I'm used to Java and I use this kind statement quite frequently as it saves writing. Is this how to do it in objective-c?

[2]

Where and how have I gone wrong here trying to get the cells to dissapear?

FeedbackTableViewController:

#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

TypeTableViewController:

#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

I'm open to better ideas on achieving what I want to achieve also so if you would consider telling me a more efficient way of doing this I would be grateful. I know delegates might be an option however I'm not confident with them yet and would thought this would be easier for me.

Was it helpful?

Solution 2

I actually managed to use this method of changing the row heights to 0.

In order to do it effectively I had to to remove the placeholder/any initial text in the rows that I didn't want shown. This required some storyboard connections which you will see named as _feedbackText _startLocation etc. When a user selected a new row, they would perform a segue to the original feedback form and therefore the viewDidAppear was called. I used this to call the [self.tableView reloadData]. Originally the change in the variable _type would not actually change anything but the heightForRowAtIndexPath is recalled when the data is reloaded.

I'm sure that using the delete row at indexPath would have worked also but I wanted to store the information that the user may have typed before they changed the type of feedback.

The New Method:

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

This will essentially hide but not get rid of the information in the text fields. This is very useful if you want to the keep any information the user typed in.

I hope this helps anyone trying to hide rows in a grouped static table view controller.

OTHER TIPS

For [1], try this and see it yourself:

int test = 3;
if(test == (1 | 2))
    NSLog(@"_MEH_");

Since it's bitwise OR operation, 0010 | 0001 equals to 0011, which is equal to 3. Hence, I would not advise you to use an operation like that. (If that's not intentional, of course).

For [2], you should use deleteRowsAtIndexPaths:withRowAnimation: call for UITableView in order to delete rows.

For example;

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

Also, don't forget to update your data source accordingly. You may want to remove some objects from

self.typeoptions

array.

P.S: Another addition, you should also change tableView:numberOfRowsInSection: since there will be less rows than 6.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top