Getting Selected and unselected button problem on cell in iPhone
Hi guys i am having a strange problem in my application. i have a custom cell for my tableview and i have a button on my cell. i have to add favorites an unfavorites functionality on that button so if that button is tapped then it will make the item favorite on that cell and button will be in selected state and if user tap it again then it will be unselected. JUST LIKE CHECK BOX. Problem that when i select one button then it 5th button al开发者_JAVA技巧so. for instance if i select button on 3rd index then it will select button on index 8 also.
any body faced this problem? Can any body help me?
The problem is because of you are reusing your cells. So when you select cell at index 0 and if there are 4 visible cells then when you scroll down cell with index 5 will reuse cell at index 0 and it will be selected too.
To resolve that issue you should always configure cells in - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
and reset to default all properties that could be changed or are individual to different cells.
I would imagine this is happening because the UITableViewCells are cached. That's what -dequeueReusableCellWithIdentifier does. In your "cellForRowAtIndexPath" function, add in some logic to set the button's state depending on whether or not the object for that row is favorited.
精彩评论