How to maintain a selected state, when clicks the UIButton in iPhone?
I have created three buttons and set the background image for the normal state and the selected state. When i click the button, changed one image as the selected state. But when is scroll the table view, the selected image is not retained (previous selected cell) which means it comes for the normal state.
My code is,
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *likeSelectedImage = [UIImage imageNamed:@"like.png"];
//set image as background for button in the normal state
[likeBtn setBackgroundImage:likeSelectedImage forState:UIControlStateNormal];
[likeBtn addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:likeBtn];
}
My Button action is,
-(void) likeAction : (id) sender
{
UIImage *likeSelectedImg = [UIImage imageNamed:@"like-selected.png"];
UIImage *likeImg = [UIImage imageNamed:@"like.png"];
if ([sender isSelected]) {
[sender setImage:likeImg forState:UIControlStateNormal];
[sender setSelected:NO];
}else {
[sender setImage:likeSelectedImg forState:UIControlStateSelected];
[sender setSelected:YES];
}
}
SO my problem is, when i scroll the t开发者_如何学JAVAable view cell, the previously selected state of an image is not retained. Because the cellForRowAtIndex method has repeatedly called, when i scrolled the table. so it automatically sets "[likeBtn setBackgroundImage:likeSelectedImage forState:UIControlStateNormal];"
. How to avoid this problem?
So please help me out?
Thanks!
You are seeing this due to the design pattern that UITableView's use for displaying data. To minimize resource usage only the table cells that are currently on screen are guaranteed to be kept in memory. As cells are scrolled off-screen the cell objects are intended to be recycled to use for the new cell's appearing at the other end of the list and therefore lose their state.
I'm assuming you've trimmed your code somewhat as you're returning a cell in tableView: cellForRowAtIndexPath:
but there's no reference to that variable anywhere else in the snippet you posted. This makes it impossible to see how you're getting the cell before adding the button.
Bit of a stab in the dark as to how your code works but here's a high level overview of what you need to do to retain state.
- Create some controller-level storage such as an NSArray to hold all your button states for all the table rows.
- In the
likeAction
determine which row the button is from (possibly assign the row number to the button's tag property when creating it) and update the state for the corresponding row in your NSArray - In
tableView: cellForRowAtIndexPath:
set up your button with the correct image by using the state which is fetched from the NSArray for the given indexPath.
the above one is not correct in syntax:
cell = [[[ToDoSummaryTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
give each cell a unique identifier, it should work.
See any sample code which uses 'reuseidentifier' in XCode IOS api.
Thanks, Bharath
I re-edit your above code. I hope, I will help you.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
likeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *likeSelectedImage = [UIImage imageNamed:@"like.png"];
//set image as background for button in the normal state
[likeBtn setBackgroundImage:likeSelectedImage forState:UIControlStateNormal];
[likeBtn setBackgroundImage:likeSelectedImage forState:UIControlStateHighlighted];
[likeBtn addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];
[cell.contentView addSubview:likeBtn];
}
You can use 'reuseIdentifier' for each cell with an identifier.
cell = [[[ToDoSummaryTableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:.<identifier>] autorelease];
精彩评论