0

in my App I've two TableViewControllers, in very first I've selected few rows randomly with a Favorite Button and saved their indexPath.row in a separate MutableArray then I Tried to access only those selected rows in second TableViewController and I used following code,

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


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   return rArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    Radio * RadioObject;
    RadioObject = [rArray objectAtIndex:indexPath.row];
    if([rFavorites containsObject:[NSNumber numberWithInt:(int)indexPath.row]])
        cell.textLabel.text = RadioObject.radioTitle;
    else
        cell.frame = CGRectZero;


    return cell;

}

it giving me follow result. when selected three cells from first TableViewController & Stored their result in a MutableArray here is its NSLog result

Favorite Radios array is (
    10,
    8,
    9
)

and now in Simulator

enter image description here]1

the thing which I am willing to do is to remove other empty cells from this TableViewController. so please suggest me how to accomplish that?

Hina Khan
  • 43
  • 8
  • 1
    see this http://stackoverflow.com/questions/14520185/how-to-remove-empty-cells-in-uitableview – Anbu.Karthik Mar 11 '17 at 10:57
  • can you NSLOG rArray and tell us the result? – Ilario Mar 11 '17 at 10:59
  • @Anbu.Karthik thats not so easy as you taking it, actually I needs to display only user selected rows, or cells, among first tableViewController which containing more than 150 rows and when user makes few rows favorites it takes only reference numbers to those rows in a mutable array not over all cell contents & when wish to go to second view controller that should display those user selected rows or cells only and I am willing to remove all unwanted or empty cells which are above & as well as bellow or in between those favorite cells. I hope it'll better clear the idea, – Hina Khan Mar 11 '17 at 11:13
  • @Anbu.Karthik I've already explored numbers of links and tried which you recently added to your comment, actually I am doing the same job since last two days. and think how many links I wold be explored & tried till now. Please come back with your code suggestions if you can figure this out. – Hina Khan Mar 11 '17 at 11:14
  • then do like take one common array , based on your condition add objects to common array, then show that arry in tableview, – Anbu.Karthik Mar 11 '17 at 11:15
  • @Anbu.Karthik infect I am retreiving data from a Json file and that contains numbers of rows and each row containing more than 4 objects, i.e id, title, image link, livestream link, So when I used to copy cell contents it only copies cell title & image but not those links which i've to use in second tableviewController too, hence I am just using reference to those rows and then retrieving whole data again in second TableVC and filtering only those favorite rows from that data and displaying. but it creates empty space for other cells too... – Hina Khan Mar 11 '17 at 11:21
  • it means problem not in secondvc, on your first vc, chck once how d u add – Anbu.Karthik Mar 11 '17 at 11:23
  • I understand to what you are trying to say, you mean to add complete cell objects in a row, instead of reference to that cell, but read my second last comment there I cleared that why I am not going with that approach. – Hina Khan Mar 11 '17 at 11:34
  • 2
    In the second table view controller, the `numberOfRowsInSection` should return `rFavorites.count`. Then in `cellForRowAt` take the value at `rFavorites[indexPath.row]` and use it to index into `rArray`. You probably want to sort `rFavorites` – Paulw11 Mar 11 '17 at 11:37
  • just set height of the row to 0 if that row don't have data... – Fahim Parkar Mar 11 '17 at 12:21
  • You need to pass the `rFavorites` array to the second `UITableViewController` and use the array passed inside the `UITableView` datasource methods – Rikh Mar 11 '17 at 12:22

4 Answers4

2

If you want to remove all other rows from tableview [show only selected row]:

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


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   return rFavorites.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    Radio * RadioObject;
    NSInteger index_rArray = [[NSString stringWithFormat:@"%@",[rFavorites objectAtIndex:indexPath.row]] integerValue];
    RadioObject = [rArray objectAtIndex:index_rArray];
    cell.textLabel.text = RadioObject.radioTitle;
    return cell;
}

If you wish to keep all rows but hide/compress then;

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath;
{
    if([rFavorites containsObject:[NSNumber numberWithInt:(int)indexPath.row]]){
    return 50; // whatever your cell size is
    }
    else {
    return 0;
    }
}
nikdange_me
  • 2,949
  • 2
  • 16
  • 24
  • `return 0;` is not working. thats the main problem I've to post question. otherwise.... Well, let me try with your first suggestion, will back in a while. – Hina Khan Mar 11 '17 at 12:48
  • Even i dont know but just try with return 1; :| – nikdange_me Mar 11 '17 at 12:49
  • Your First method's Following line `NSInteger index_rArray = [rFavorites objectAtIndex:indexPath.row];` giving following issue `incompatible pointer to integer conversion NSInteger (AKA Long) with an expression of type id` So any idea why it is giving that issue??? – Hina Khan Mar 11 '17 at 12:53
  • ok.. replacewith [[NSNumber numberWithInt:[rFavorites objectAtIndex:indexPath.row] intValue]; – nikdange_me Mar 11 '17 at 12:55
  • thing to mention clearly here, my `rFavorites` array contains `NSNumber` Object in its `indexpath.row` – Hina Khan Mar 11 '17 at 12:57
  • @HinaKhan i have made correction in code please do update!! – nikdange_me Mar 11 '17 at 13:01
  • @ElTomato what is wrong ?? please tell me.. i will correct it – nikdange_me Mar 11 '17 at 13:16
  • @nickdange_me , Thanks Alot, this time it is working perfectly, giving favorite rows without empty cells, it works. Meanwhile I've another query, not Directly related to this specific question but partially related to this, infect when I goes back via tabController(nor navigationController) and make changes in my favorite cells selection, and comes back to this concerend tableViewController, it is not showing any changes unless I goes back and re-open both viewcontrollers. any idea why it is behaving like this? – Hina Khan Mar 11 '17 at 14:19
  • I you need a cell to be invisible (or 0 height), you don't need the cell at all. But i might have misunderstood some of your logic. – silentBob Mar 11 '17 at 14:55
  • @HinaKhan what are you using navigationController or tabController?? – nikdange_me Mar 11 '17 at 15:56
  • I am using Navigation Controller initially to reach my firstTableViewController because earlier there are few more ViewControllers which I used to embed in NavigationViewController, & finally both of the Tableviewcontrollers which are in this discussion I used to embed both in TabController. – Hina Khan Mar 12 '17 at 05:36
  • ok.. this simply mean toyou jump between this two view controller through tabController tab.. right!? @HinaKhan – nikdange_me Mar 12 '17 at 14:46
  • yes. when I jump back to first tableviewcontroller where I used to shortlist(favorites) few rows to view in second viewcontroller, in very first approach, it gives accurate numbers of rows but when I goes back and make some alteration in my favorite rows and then goes to second view controller there's no change . . . any idea? – Hina Khan Mar 12 '17 at 17:33
  • if you are using tabcontroller; tableview is loaded only when you enter in it for first time! so you have to call [_tableview reload] in viewWillAppear Method of secondviewcontroller, when make change in your firstview controller selection @HinaKhan – nikdange_me Mar 13 '17 at 07:19
  • there is no `viewWillAppear` method in my `SecondViewController`, but `viewDidLoad` where I used to load my table view for the first time when it loads. – Hina Khan Mar 13 '17 at 07:25
  • So add it!! and reload tableview when you change in firstViewController – nikdange_me Mar 13 '17 at 07:27
  • I am unable to do it correctly, as I tried for adding viewWillAppear method like this `-(void)viewWillAppear { [self retrieveData]; }` but it does nothing. – Hina Khan Mar 13 '17 at 07:48
  • as I've defined `[self.tableView reloadData];` within that `retrieveData` method. so I needed to call that here in viewWillAppear method. – Hina Khan Mar 13 '17 at 07:50
  • meanwhile I am also planing to remove tabViewController and to add both of the viewControllers in NavigationViewController, please suggest me whether it'll be the right approach to figure out this problem? – Hina Khan Mar 13 '17 at 07:54
  • I was doing it wrongly, this time I used following `- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self retrieveData]; }` and after that when I tested my program, it is working as expected. Thanks for your time & suggestions. – Hina Khan Mar 13 '17 at 08:04
  • Welcome :) @HinaKhan – nikdange_me Mar 14 '17 at 14:11
  • @nikdange_me yes Voted up for your answer. – Hina Khan Mar 14 '17 at 14:11
0

Try this one code.

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


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return rFavorites.count;
}


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

Radio * RadioObject;
RadioObject = [rArray objectAtIndex:[rFavorites[indexPath.row]]];
    cell.textLabel.text = RadioObject.radioTitle;


return cell;

}
Sakir Sherasiya
  • 1,562
  • 1
  • 17
  • 31
  • following line `RadioObject = [rArray objectAtIndex:[rFavorites[indexPath.row]]]; ` giving error `Expected Identifier` – Hina Khan Mar 11 '17 at 12:33
  • You almost have the right answer here, except that in rFavorites contains NSNumbers, so you can't use it as a direct array index; you need to get the NSNumber and take its intValue – Paulw11 Mar 11 '17 at 21:39
0

Actually what you are doing is wrong, you should create new array for the favorite because may be later the first array will get updated and as you just have the reference as row number, the second array of reference is useless.

To answer your question, just set height of the row to 0 when you don't have reference number...

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath      *)indexPath;
{
    if (don't have reference) return 0;
    return your_default_height
}
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
0

Did you try not adding them in at all? :)

Maybe you could switch source arrays when switching tables, so when numberOfRows gets called on reload, tableView will reload with the right array, and display right amount of cells.

Then you wont need to hack like with setting cell.frame to CGZero. Although, that hack might work if you set heightForRow to UITableViewAutomaticDimension.

silentBob
  • 170
  • 9