2

I am new to the iPhone development. I got stuck with a problem. I want a check box function implemented in my UITableView. But my UITableViewCells are custom cell which consist of image and 3 UILabels. I could bring a sub view to the table view so that check box image can placed and I am successful in that. But the problem comes is whenever I click on the image only the last cell check box get changed. I would like to access the cell which I've clicked.

I have tried this

    UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]

But this crashes since cell is custom.

Can any one suggest me a good method to do that?

Alan Haggai Alavi
  • 72,802
  • 19
  • 102
  • 127
user1288402
  • 35
  • 2
  • 7
  • post more code. post the error messages from when it crashes. – Ali Apr 02 '12 at 12:31
  • 1
    Why do you want to access the cell that is being touched? You should just be dealing with the DATA, not the CELL. The cell is just there to display the data and the data is stored in your model (search MVC if you are unsure). – Nick Bull Apr 02 '12 at 12:34
  • Using `- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath` you'll have selected index, using `indexPath.row` You'll get the cell which user selected. – Hemang Apr 02 '12 at 12:39

3 Answers3

9

Instead of using

UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath]

try using

YourCustomCell *cell = (YourCustomCell *)[self.tableView cellForRowAtIndexPath:indexPath];

Hope it helps

Novarg
  • 7,390
  • 3
  • 38
  • 74
  • @user1288402 you are welcome. Please do not forget to mark the answer as correct if it helped. It will encourage the users to answer your questions in the future. – Novarg Apr 03 '12 at 12:40
2

First of all you need to define a tag -

#pragma imageViewTag 1

then in your cellForRowAtIndexPath assign this tag to your image view

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

then you can access your image view any where with the help of this tag as -

 UITableViewCell *cellView = (UITableViewCell*) [tblView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];

 UIImageView *imgView = (UIImageView*) [cellView viewWithTag:imageViewTag];
saadnib
  • 11,145
  • 2
  • 33
  • 54
1

In your TableView DidSelect Delegate method

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

   UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   UIImageView *image = [cell.contentView viewWithTag:15];
   //You should have set the tag value as 15  when creating the image in cell 
   //and you should have added to the cell contentview

}

If you cant able to get the cell then probably you wouldnt have used the reusable cell concept correctly. So post your entire cellforrowindex code.

If you are using the check kind of thing. Why dont you use the default disclosure indicator of tableview instead of your image.

ipraba
  • 16,485
  • 4
  • 59
  • 58