Reputation: 1695
I have button on uitableviewcell which have target on function like this:
likeButton?.addTarget(self, action: "likeButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside)
inside the function I set my button title like this:
sender.setTitle("\(addedLikeCount) Likes", forState: UIControlState.Normal)
But whenever I scroll the view up or down, my button title change to it's default. Why is that happen? Is there any way I can fix this without reloading table?
Feel free to give me any advice, doesn't matter in swift or objective c.
UPDATE
So I did code below on my function:
self.likeArray.replaceObjectAtIndex(index!, withObject: addedLikeCount)
sender.setTitle("\(self.likeArray[index!]) Likes", forState: UIControlState.Normal)
And this on my uitableviewcell:
var totalLike = likeArray[indexPath.row] as? String
currentLikeCount = totalLike
likeButton?.setTitle("\(totalLike)", forState: UIControlState.Normal)
It worked, but when I scroll the tittle become default again
Upvotes: 2
Views: 6991
Reputation: 2413
Tableview cell reloads everytime it's will be shown on the screen after scroll. You must provide reusable identifier for your cell on you storyboard or XIB Objective-C variant:
- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"your_cell_identifier_on_storyboard_or_cell_xib";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
/// datasource code here
return cell;
}
If you want to store all your context for specific indexpath, as on your question - to hold all buttons caption, use
dequeueReusableCellWithIdentifier: forIndexPath:
instead of
dequeueReusableCellWithIdentifier:
Warning: dequeuing cells for specific indexpath may slow performance
Upvotes: 2
Reputation: 1081
As Oyeoj commented
Cell is changing because "the cell is being reuse and therefore refreshed to its original setup under cellForRowAtIndexPath
, solution is to have a global variable then setup on reload under cellForRowAtIndexPath
."
For your desired task you have to store selected cell index some where i.e. your button on someIndex
cell has Likes
title.
for Objective-C
in .h
NSMutableArray likeArray;
in .m
in viewDidLoad
likeArray=[[NSMutableArray alloc]init];
in cellForRowAtIndexPath
check
if ([likeArray containObject:[NSNumber numberWithInteger:indexPath.row]])
{
[button setTitle:@"Likes", forState: UIControlStateNormal];
}
else
{
[button setTitle:@"Some Other Title", forState: UIControlStateNormal];
}
in didSelectRowAtIndexPath
-
NSNumber *cellIndex=[NSNumber numberWithInteger:indexPath.row];
if(![likeArray containObject:cellIndex])
{
[likeArray addObject:cellIndex];
}
[table reloadData];
Upvotes: 0
Reputation: 9148
Do you use this in your code ?
tableView.dequeueReusableCellWithIdentifier("reuseIdentifer")
If yes, you have to save the state of each cell.
Because every time you scroll up and down, TableView will bring back the previous cell that outside of the screen.
What you need is setting the new state of the cell correspond to indexPath
in cellForRowAtIndexPath
method something like this
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
if let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifer") as? UITableViewCell{
cell.title = titles[indexPath.row]
return cell
}
...
...
}
Upvotes: 1
Reputation: 3690
Create a function which check for which row the title is changed and vice versa.For eg
-(BOOL)checkForSelectedRow:(NSIndexPath *)path
Now Create array which store for which button the title is changed,So whenever the button is tapped u add it to the array in your likeButtonTapped
Now in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if([self checkForSelectedRow:indexPath]){
//change the title of button
}
else{
//default of button
}
Upvotes: 0