2012-01-26 41 views
0

我有一個UItableView,每個單元格上有兩個按鈕。您可以從celltextLabel中增加或減去1。我想補充細胞的電流值+1本:將cell.textLabel.text值添加到數組並顯示它

- (IBAction)addLabelText:(id)sender{ 
    num = [NSString stringWithFormat:@"%d",[cell.textLabel.text intValue] +1];//<--- num is an NSNumber 
    number = [[NSMutableArray alloc]initWithObjects:num, nil];//<---- number is an NSMutableArray 
    [myTableView reloadData]; 

} 

,我試圖減去文本,並將其存儲在這個數組:

- (IBAction)subtractLabelText:(id)sender 
{ 
     if ([[cell.textLabel text] intValue] == 0){  


    num = [NSString stringWithFormat:@"%d",[num intValue] +0]; 
    [number addObject:num]; 
     [myTableView reloadData]; 

    } 
    else{ 
    num = [NSString stringWithFormat:@"%d",[num intValue] -1]; 
    [number addObject:num]; 
    [myTableView reloadData]; 
    } 
} 

和IM試圖設置cell.textLabel.text在在cellForRow這樣的:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath 
{ 
    static NSString *identifier = @"Cell"; 

    cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
    if (cell == nil) 
    { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; 


    } 
    cell.textLabel.text = [number objectAtIndex:indexPath.row];//<---IM USING THIS LINE TO SET THE NEW TEXTLABEL 
    cell.textLabel.text = @"1"; 


return cell; 
} 

我的問題

所以,加法運作,但減法沒有。當我按下單元格上的按鈕時,它根本不起作用。提前致謝!!

回答

1

冒着指出顯而易見的...你似乎已經將文本屬性硬編碼爲@「1」。

cell.textLabel.text = [number objectAtIndex:indexPath.row];//<---IM USING THIS LINE TO SET THE NEW TEXTLABEL 
cell.textLabel.text = @"1"; 

第一行可能是在做你認爲的......但是你立即將它改回@「1」。

編輯 - 根據評論中的說明,這是我認爲你想要做的。我會修改你自己的代碼張貼。

請注意,我把我的添加到viewDidLoad作爲一個例子。你可以在初始化或任何其他地方做到這一點,無論你知道要顯示多少個單元格。

- (void)viewDidLoad { 
    self.number = [[[NSMutableArray alloc] init] autorelease]; 
    for (int i = 0; i < [HOW MANY CELLS DO YOU WANT?]; i++) { 
     [self.number addObject:@"1"]; 
    } 

    [myTableView reloadData]; 
} 

- (IBAction)addLabelText:(id)sender { 
// Note that I'm assuming here that your button is a direct child of the cell. 
// If not, you'll need to change this. 
    UITableViewCell *cell = [sender superview]; 
    NSInteger newNumber = [cell.textLabel.text intValue] + 1; 
    NSString *newNumberString = [NSString stringWithFormat:@"%d", newNumber]; 

    [self.number replaceObjectAtIndex:cell.tag withObject:newNumberString]; 

    [myTableView reloadData]; 
} 

- (IBAction)subtractLabelText:(id)sender { 
// Note that I'm assuming here that your button is a direct child of the cell. 
// If not, you'll need to change this. 
    UITableViewCell *cell = [sender superview]; 
    NSInteger newNumber = [cell.textLabel.text intValue] - 1; 
    newNumber = (newNumber < 0) ? 0 : newNumber; 

    NSString *newNumberString = [NSString stringWithFormat:@"%d", newNumber]; 

    [self.number replaceObjectAtIndex:cell.tag withObject:newNumberString]; 

    [myTableView reloadData]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *identifier = @"Cell"; 

    cell = [tableView dequeueReusableCellWithIdentifier:identifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; 
    } 

    cell.tag = indexPath.row; // Important for identifying the cell easily later 
    cell.textLabel.text = [self.number objectAtIndex:indexPath.row]; 

    return cell; 
} 

我並不想深入太多多,但我會建議你看一看reloading only the cell modified,而不是調用[myTableView reloadData]每次。

+0

好吧,我應該刪除這一行嗎?我需要它在單元格創建後立即顯示'1',我似乎也無法做到這一點。有另一個地方我可以把這條線?感謝您的答覆!! – iProRage

+0

@iProRage - 根據您發佈的代碼來判斷您的意圖有點難。從我所看到的情況來看,這不會更新您觸摸的值的單元格,但是索引值處的單元格的值將被添加到「數字」處。如果您打算更改被觸摸的單元格的值,則需要事先創建數字數組,每個單元格索引的初始值爲@「1」,然後更改單元格正確索引處的值感動。 – DougW

+0

我只是想給單元格加1和減1。我之前做過這個,沒有將值添加到數組中,當我將一個單元格從視圖中滾動出來並返回時,值都被搞砸了。現在即時將它添加到數組中,因爲我讀到它會解決我的問題。我應該在哪裏設置數組中'1'的初始值? cellForRow方法或viewDidLoad?再次感謝道格! – iProRage

相關問題