0
我有一個UItableView,每個單元格上有兩個按鈕。您可以從cell
的textLabel
中增加或減去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',我似乎也無法做到這一點。有另一個地方我可以把這條線?感謝您的答覆!! – iProRage
@iProRage - 根據您發佈的代碼來判斷您的意圖有點難。從我所看到的情況來看,這不會更新您觸摸的值的單元格,但是索引值處的單元格的值將被添加到「數字」處。如果您打算更改被觸摸的單元格的值,則需要事先創建數字數組,每個單元格索引的初始值爲@「1」,然後更改單元格正確索引處的值感動。 – DougW
我只是想給單元格加1和減1。我之前做過這個,沒有將值添加到數組中,當我將一個單元格從視圖中滾動出來並返回時,值都被搞砸了。現在即時將它添加到數組中,因爲我讀到它會解決我的問題。我應該在哪裏設置數組中'1'的初始值? cellForRow方法或viewDidLoad?再次感謝道格! – iProRage