2015-06-22 52 views
2

我有UITableViewCell的具有目标的功能这样的按钮:为什么uitableviewcell在滚动时发生变化?

likeButton?.addTarget(self, action: "likeButtonTapped:", forControlEvents: UIControlEvents.TouchUpInside) 

设置我的按钮标题是这样的函数内部:

sender.setTitle("\(addedLikeCount) Likes", forState: UIControlState.Normal) 

但每当我滚动视图向上或向下,我的按钮标题更改为默认值。为什么会发生?有没有什么办法可以解决这个问题,而无需重新加载表?

随时给我任何意见,在迅速或客观的c。无所谓。

UPDATE

所以我做了下面的代码在我的功能:

self.likeArray.replaceObjectAtIndex(index!, withObject: addedLikeCount) 

sender.setTitle("\(self.likeArray[index!]) Likes", forState: UIControlState.Normal) 

这对我的UITableViewCell:

var totalLike = likeArray[indexPath.row] as? String 

currentLikeCount = totalLike 

likeButton?.setTitle("\(totalLike)", forState: UIControlState.Normal) 

它的工作,但是当我滚动丝毫不差成为再次默认

+0

因为细胞正在被重用,因此刷新至下'-cellForRowAtIndexPath',解决原来的设置是有一个全局变量,然后下重装安装'-cellForRowAtIndexPath' – 0yeoj

+0

我该怎么办,没有reloaddata? –

+0

获得superview(单元格)被按下的'likeButton'是,然后更改/设置标题,但不重新加载表并不滚动否则它将被重用并再次刷新,这是tableview的自然行为(用于内存处理)..在你的情况意思..这是你唯一的选择.. :) – 0yeoj

回答

0

As Oyeo j评论

单元正在改变,因为“该单元正在被重用并因此被刷新到cellForRowAtIndexPath下的原始设置,解决方法是在全局变量上设置,然后在cellForRowAtIndexPath下重新加载时设置。

对于您所需的任务,您必须将选定的单元格索引存储在某个位置,即您的someIndex单元格上的按钮具有Likes标题。

中.H

NSMutableArray likeArray;

在.M

为目标C

viewDidLoad

likeArray=[[NSMutableArray alloc]init];

cellForRowAtIndexPath校验

if ([likeArray containObject:[NSNumber numberWithInteger:indexPath.row]]) 
{ 
    [button setTitle:@"Likes", forState: UIControlStateNormal]; 
} 
else 
{ 
    [button setTitle:@"Some Other Title", forState: UIControlStateNormal]; 
} 
didSelectRowAtIndexPath

-

NSNumber *cellIndex=[NSNumber numberWithInteger:indexPath.row]; 
if(![likeArray containObject:cellIndex]) 
{ 
    [likeArray addObject:cellIndex]; 
} 
[table reloadData]; 
+0

我编辑了我的问题,我已经尝试过你的解决方案,但它不适合我。 –

+0

答复已更新。 –

+0

它应该与Check Box Accessory按钮相同。 –

0

创建该检查哪一行的标题被改变,并且副versa.For例如,功能

-(BOOL)checkForSelectedRow:(NSIndexPath *)path 

立即创建阵列,其用于存储哪个按钮的标题改变,所以无论何时按下按钮ü将它添加到您的likeButtonTapped

现在

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

if([self checkForSelectedRow:indexPath]){ 
//change the title of button 

} 
else{ 
//default of button 
} 
1

你在代码中使用呢?

tableView.dequeueReusableCellWithIdentifier("reuseIdentifer") 

如果是,则必须保存每个单元格的状态。

因为每当你上下滚动时,TableView都会返回屏幕以外的前一个单元格。

你需要的是将小区设置的新状态是什么对应indexPathcellForRowAtIndexPath方法是这样的

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    if let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifer") as? UITableViewCell{ 

     cell.title = titles[indexPath.row] 
     return cell 

    } 
    ... 
    ... 
} 
+0

它给了我一个错误,xcode无法找到返回,如果我把回报里面,如果语句 –

+0

把返回UITableViewCell()在函数的结尾 – nRewik

2

泰伯维细胞重新加载每次它会滚动后,屏幕上显示。 你必须为你的你故事情节,或者提供可重复使用的标识符XIB Objective-C的变种:

- (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; 

} 

如果你想,以储存所有的上下文具体indexpath作为对您的问题 - 来容纳所有按钮的标题,使用

dequeueReusableCellWithIdentifier: forIndexPath: 

,而不是

dequeueReusableCellWithIdentifier: 

警告:离队细胞对特定indexpath可能会降低性能

相关问题