2012-01-26 3 views
0

私はUItableViewを持っていますが、各セルに2つのボタンがあります。 celltextLabelから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]; 

} 

と私はテキストを減算し、これに配列に保管しようとしています:私はこれで細胞を現在値+1を追加cell.textLabel.textで設定しようとして

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

とイムこのような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"に戻しています。

EDIT - コメントの明確化に基づいて、ここにあなたがしたいと思うものがあります。投稿されたコードを修正します。

例としてviewDidLoadに追加しました。あなたは何個のセルを表示したいのか知っていれば、initやその他の場所でこれを行うことができます。

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

私はあまり多くの深さで取得する必要はありませんが、私はあなたの代わりに[myTableView reloadData]たびに呼び出すので、reloading only the cell modifiedを見てみましょうお勧めします。

+0

大丈夫ですので、この行を削除する必要がありますか?私は、セルが作成されるとすぐに '1'を表示する必要があります。私はその行を置くことができる別の場所はありますか?返信してくれてありがとう!! – iProRage

+0

@iProRage - 投稿したコードだけであなたの意図が何であるかを知るのは少し難しいです。私が見ていることから、これはあなたがタッチしたセルを更新するのではなく、値が 'number'に追加されるインデックスのセルの値です。タッチされたセルの値を変更する場合は、各セルのインデックスの初期値として@ "1"を使用して数値配列を事前に作成してから、そのセルの正しいインデックスの値を変更する必要があります触れた – DougW

+0

私はちょうどセルに「1」を加えたり引いたりしようとしています。私は配列に値を追加せずに前にこれを行いました。そして、ビューからセルをスクロールして戻ったときに値がすべて乱されました。そして今、私はそれが私の問題を解決することを読むので、配列にそれを追加します。そして、どこで配列の '1'の初期値をどこに設定すべきですか? cellForRowメソッド、またはviewDidLoad?もう一度おねがいします! – iProRage

関連する問題