2013-07-05 26 views
1

所以我在我的Uitableview中的每個表格單元格中嵌入了兩個按鈕。我允許用戶編輯按鈕標題。然而,只要用戶從屏幕上滾動單元格,標題就會再次變爲空白。任何想法如何解決這個問題?這裏是我的代碼:滾動表格時按鈕文字消失

的TableView方法:

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

    static NSString *cellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    if(cell == nil){ 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier]; 
    } 

    addWeightButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    addRepButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    [addWeightButton addTarget:self action:@selector(weightPressedAction:) forControlEvents:UIControlEventTouchUpInside]; 
    [addRepButton addTarget:self action:@selector(repPressedAction:) forControlEvents:UIControlEventTouchUpInside]; 
    addWeightButton.frame = CGRectMake(110.0f, 5.0f, 75.0f, 30.0f); 
    addRepButton.frame = CGRectMake(260.0f, 5.0f, 50.0f, 30.0f); 
    [addWeightButton setTag:indexPath.row]; 
    [addRepButton setTag:indexPath.row]; 
    [cell addSubview:addWeightButton]; 
    [cell addSubview:addRepButton]; 
    return cell; 
} 

編輯按鈕標題方法

-(void) editWeightNumber:(int)value { 
    [[routine objectAtIndex:index] addWeight:value atIndex:selectedWeightBox]; 
    NSString* weights = [NSString stringWithFormat:@"%i", [[routine objectAtIndex:index] getWeight:selectedWeightBox]]; 
    [weightSender setTitle:weights forState:UIControlStateNormal]; 

} 
+0

我沒有看到任何代碼,在'tableView:cellForRowAtIndexPath:'方法中設置按鈕的標題,在那裏你正在創建新的按鈕實例。每次使用屏幕上的按鈕滾動單元格時,再次打開,即使單元格正在重新使用,按鈕也會重新創建。您需要實現跟蹤每個單元的按鈕標題以及保存其他單元數據的集合。 – bobnoble

+0

啊,我明白了。管理弄清楚。謝謝! –

回答

0

之前插入return cell東西沿着這個思路的線路:

id routineObject = [routine objectAtIndex:indexPath.row]; 
NSString *weights = [NSString stringWithFormat:@"%i", [routineObject getWeight:addWeightBox]]; 
[addWeightButton setTitle:weights forState:UIControlStateNormal]; 
NSString *reps = [NSString stringWithFormat:@"%i", [routineObject getWeight:addRepBox]]; 
[addRepButton setTitle:reps forState:UIControlStateNormal]; 

[編輯:哦,我的,Stackoverflow告訴我這個問題不到5分鐘...必須已經有一些時鐘問題]