0

I use UITableView with prototype cells and would like the top most cell to be an "adder" cell: when user taps on that cell, the segue is being performed. As the UITableView is being dynamically generated (except the top one) and cells are being reused, I have two question:

1 - is it possible to make one static cell while all the others will remain prototype cells?

2 - If not, how to implement this solution? I think, it has to be implemented in

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

but I have no idea, is this method right place to do it. Am I correct, to implement this custom cell, I need to check, whether indexPath.row = 0 and substitute the needed cell with custom, while programmatically shifting all the cells one row down?

Kampai
  • 22,848
  • 21
  • 95
  • 95
Richard Topchii
  • 7,075
  • 8
  • 48
  • 115

1 Answers1

1

Yiu can do on the Tap of Your cell, you can create two different cell, one your prototype cell and one for your details

in your

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        AdderCell *cell = (AdderCell*)[tableView dequeueReusableCellWithIdentifier:@"AdderCell" forIndexPath:indexPath];
     } else {
       CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:@"CustomCell" forIndexPath:indexPath];
     }
}

on tap of your Adder cell in

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
                // add data to your row array and reload the tableRow
             } else {
               // do your tap action
             }
     }
Retro
  • 3,985
  • 2
  • 17
  • 41
  • Thank you for explanation. Few comments: firstly, you misunderstood me a bit. Custom cell will just perform a segue with parameters, so there is no need to reload anything. Secondly, I have a question about `- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath` - All the TableView is being attached to CoreData and FetchedResultsController. FRC gives index path as 0 - CDObject0, 1 - CDObj1... But if there is no objects in CD, it does not call the method tableViewcellForRowAtIndexPath. Do I need to call it manually on viewDidLoad? – Richard Topchii Jul 31 '14 at 12:50
  • And do I need to override some method to make indexPath: 0 - custom, 1- CDObj0, 2 - CDObj1? – Richard Topchii Jul 31 '14 at 12:50
  • if you are using FetchedResultsController then it will automatically reload the table if you have implemented it's delegate! – Retro Jul 31 '14 at 13:05
  • Yep, but how about CoreData shift? Do I need to do it, or it also will be done automatically? – Richard Topchii Jul 31 '14 at 13:08
  • suppose if your are have 10 records sorted by id like 10,9,8.......1, if you add new coredata object with id 11 then it will be on top. if you have another scenario then get your fetch data into a MutableArray and add new object into top! – Retro Jul 31 '14 at 13:11
  • Got it. But e.g. I don't have any records for some entity in CoreData yet, but I want button to exist. I tried to call `NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0]; [self tableView:self.tableView cellForRowAtIndexPath:indexPath];` but the app crashes with exception `'NSInternalInconsistencyException', reason: 'request for rect at invalid index path ( {length = 2, path = 0 - 0})'` – Richard Topchii Jul 31 '14 at 13:24
  • where do you want this button? – Retro Jul 31 '14 at 13:29
  • At the top of UITableView, notwithstanding how much there Cells – Richard Topchii Jul 31 '14 at 13:30
  • from numberOfRows method return your count+1 and add the static cell and your for rest of object use indexpath.row - 1 – Retro Jul 31 '14 at 13:32
  • Thanx, my mistake. And what do you mean by static cell? Can I use static cells and dynamic prototypes simultaneously? – Richard Topchii Jul 31 '14 at 13:36
  • 1
    Yes, in a way! look more here http://stackoverflow.com/questions/9322885/combine-static-and-prototype-content-in-a-table-view – Retro Jul 31 '14 at 13:38
  • Same problem, same solution. Actually, cells in the solution mentioned above are not purely "Static", they're still dynamic, but without adding the dynamic content. Thank you for the help, problem solved! – Richard Topchii Jul 31 '14 at 13:42