开发者

how to display table view cells whenever we need to click on one cell

i am writing one app in that app i have to display tableview with two cells.while the first cell touched by the user then it have to show some other cells within the view (i cant use navigation here) and also second cell also visible..can any开发者_JS百科 one tell me the suggestion for this. thank you all.


If you want to insert table view cells when you are clicking on a cell, try the following code.

Lets consider you are going to insert 3 rows in the position 1, 2 and 3 in the section 0. In the didSelectRowAtIndexPath method, do the following. Here the rows will be inserted in section 0, if we select row 0(first row) in section 0(first section).

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

   if (indexPath.row == 0 && indexPath.section == 0) {

       // Create indexPaths for the rows you are going to insert

       NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:1 inSection:0]];
       NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:2 inSection:0]];
       NSIndexPath *indexPath3 = [NSIndexPath indexPathForRow:3 inSection:0]];

       // Increase the number of rows in section 0 by 3, as we are inserting 3 rows here

       numberOfRowsInSectionZero = numberOfRowsInSectionZero + 3;

       // Insert the rows

       [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath1, indexPath2, indexPath3, nil] withRowAnimation:UITableViewRowAnimationFade];
    }
}

In your numberOfRowsInSection method, you should be having something like the following,

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {

    // some code here

    if (section == 0) return numberOfRowsInSectionZero;

    // some code here
}

And make sure you are having appropriate code in cellForRowAtIndexPath method to display content in the newly displayed rows.

Note: You have to alter your code in such a way that the rows are not inserted when clicking the row 0 of section 0, when they are already been inserted.


make a bool (isRowClick) variable and

set YES in didSelectRow and then write this line

[yourTable reloadData];

and

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

if(isRowClick)
  return 7;//according to you  
else
  return 2;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

  isRowSelect=YES;
 //stuff
}

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

//stuff according to you
}

this will hep you.


  1. define a data-object e.g. NSMutableDictionary
  2. always readout the data from this object (for count and also for the values you want to show)
  3. in the case the user clicks the defined cell. you'll flush the MutableDirectory and add new data.
  4. Reload the TableViewController [tableviewController.tableview reloadData];

thats it cheers Simon

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜