1

I'm using a UITableView and a custom cell with a checkbox. I have more than 1 section. When a check a checkbox in the first section, for example the cell with row = 0 and section = 0, I save the data and it works. But the cell in the row = 0 and section = 1 is also checked! How can I make the difference between those sections ?

Thank you so much!

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Lapinou
  • 1,467
  • 2
  • 20
  • 39

2 Answers2

3

Following sample code will help you for your situation.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";

    CustomCell *cell = (CustomCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
       cell = (CustomCell *) [[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil] objectAtIndex:0];
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.checkBox = [self fillCheckBoxStatus:indexPath];//This method will say about check box whether going to SET or NOTSET. 
    //...
    return cell;
}
xapslock
  • 1,119
  • 8
  • 21
Mani
  • 17,549
  • 13
  • 79
  • 100
-2

use dequeueReusableCellWithIdentifier to nil like bellow...

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:nil];
    if (cell == nil) {

    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil] autorelease];
      ////you another code..
   }
  return cell;
}
Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • Then why you need `dequeueReusableCellWithIdentifier`. Create the cell and return. You can save 3 line of code – Anil Varghese Apr 19 '13 at 12:13
  • no here it required please read documents about that method of UITableViewCell... – Paras Joshi Apr 19 '13 at 12:14
  • 1
    `dequeueReusableCellWithIdentifier` is used to reuse the cell. By passing nil you are not reusing any cells. So `dequeueReusableCellWithIdentifier` is really not required. If it requires __do not pass nil as argument__. Please read the documentation of [dequeueReusableCellWithIdentifier](http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableView_Class/Reference/Reference.html).. It says __identifier :A string identifying the cell object to be reused. This parameter must not be nil.__ – Anil Varghese Apr 19 '13 at 12:27
  • i know everything dude.. its problem with reuse of cell.. once tableview scroll after here dynamically you see checked and unchecked checkbox every time diffferent.. but if you pass nil then its use every cell once per data... not reuse agian that cell and you seen the perfect data... :) – Paras Joshi Apr 19 '13 at 12:30
  • I got your point. I just mention an optimization, if you are not reusing cell you can skip that step. That all!! Anwy i didnt down vote ur answer. Done by some one else – Anil Varghese Apr 19 '13 at 12:42
  • ok no problem but here why i used it because in future or now if some user see direct short code that confused that what is this.. so i write thease whole code so user easyly understand and use this.. so... :) – Paras Joshi Apr 19 '13 at 12:44
  • @Anil also try my code and your code after tell me what u get different.. :) – Paras Joshi Apr 19 '13 at 12:48
  • I didnt answer for this question. Your answer make any difference better than just create and return cell – Anil Varghese Apr 19 '13 at 12:59
  • @ParasJoshi : can u pls take a look at my question... http://stackoverflow.com/questions/17911314/tag-not-working-in-uitableview – Fahim Parkar Jul 28 '13 at 19:52