2011-11-30 28 views
1

私はテーブルビューを持っています。私はもともとセルのtextLabelを使ってこれを処理していましたが、それはうまくいきませんでした。プログラムごとに各セルにUILabelを追加し、2つのボタンを追加ボタンと押しボタンを押すと基本的にカウントダウンします。 (アップまたはダウン1経つ)は、2つのボタンのための方法がある:私はボタンを押下し、複数のセルを追加したとき私のテーブルビューに奇妙なことが起こっています

- (IBAction)addLabelText:(id)sender{  
    cellLabel.text = [NSString stringWithFormat:@"%d",[cellLabel.text intValue] +1]; 

} 

- (IBAction)subtractLabelText:(id)sender 
{ 
    if ([[cellLabel text] intValue] == 0){ 
     cellLabel.text = [NSString stringWithFormat:@"%d",[cellLabel.text intValue] +0]; 
    } 
    else{ 
     cellLabel.text = [NSString stringWithFormat:@"%d",[cellLabel.text intValue] -1]; 
    } 
} 

は、ボタンは、互いのラベルに干渉しました。だから私は、上記の各メソッドにこのコード行を追加しました。 :

cellLabel = (UILabel*)[sender superview];  

で、問題を修正することを期待しています。しかし、実際には、私は再びtextLabelセルを信じていました。ここに、新しい行が追加されたボタンの1つを押す前と後の様子を写真で示します。

enter image description here

後:

enter image description here

細胞のUILabel年代互いに干渉するのボタンを固定して任意の助け助けてください! :あなたのコードはまだ細かい動作しても、あなたは間違ってそれをキャスト理由ですので、またtext性質を持っているのUITableViewCellであなたのUIButtonのDおかげ

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

    cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) 
    { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
     newBtn = [[UIButton alloc]init]; 
     newBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [newBtn setFrame:CGRectMake(280,10,25,55)]; 
     [newBtn addTarget:self action:@selector(subtractLabelText:) forControlEvents:UIControlEventTouchUpInside]; 
     [newBtn setTitle:@"-" forState:UIControlStateNormal]; 
     [newBtn setEnabled:YES]; 
     [cell addSubview:newBtn]; 

     subBtn = [[UIButton alloc]init]; 
     subBtn=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [subBtn setFrame:CGRectMake(250,10,25,55)]; 
     [subBtn addTarget:self action:@selector(addLabelText:) forControlEvents:UIControlEventTouchUpInside]; 
     [subBtn setTitle:@"+" forState:UIControlStateNormal]; 
     [subBtn setEnabled:YES]; 
     [cell addSubview:subBtn]; 

     cellLabel = [[UILabel alloc]init]; 
     [cellLabel setFrame:CGRectMake(85, 22, 27, 27)]; 
     [cellLabel setText:@"1"]; 
     [cell addSubview:cellLabel];** 
    } 
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; 
    cell.imageView.image = [imageArray objectAtIndex:indexPath.row];  
    // cell.textLabel.text = [cells objectAtIndex:indexPath.row]; 
    **if ([cell.imageView.image isEqual:[UIImage imageNamed:@"paddle5.png"]]) { 
     [cellLabel setFrame:CGRectMake (195, 22, 30, 30)]; 
    } 
    if ([cell.imageView.image isEqual:[UIImage imageNamed:@"paddle4.png"]]) { 
     [cellLabel setFrame:CGRectMake (170, 22, 30, 30)]; 
    } 
    if ([cell.imageView.image isEqual:[UIImage imageNamed:@"paddle3.png"]]) { 
     [cellLabel setFrame:CGRectMake (140, 22, 30, 30)]; 
    } 
    if ([cell.imageView.image isEqual:[UIImage imageNamed:@"paddle2.png"]]) { 
     [cellLabel setFrame:CGRectMake (110, 22, 30, 30)]; 
    }** 
return cell; 
} 
+1

iOS5用にビルドする場合は、2つのUIButtonの代わりにUIStepperを使用することができます。 –

答えて

1

あなたが設定したセルにタグを設定する: 私のテーブルには私はいくつかのセクションとすべてのセクションにいくつかのセルがあります。したがって、最初の(0)セクションのセルにはタグ0〜9999があります。 2番目から - 10000 ... 19999 adn so on。私はセルのタグ= 10000 *セクションの数このセクションのこのセルの生のPLUS番号。どのセルが何であるかを計算するために、10000個のタグが何個あるのか、どのセクションから計算するのか、そして何百万個ものタグが何であるのかを計算する必要があります。だから、私はセクションビューとローテーブルビューを持っています。

cell.contentView.tag = indexPath.row+10000*indexPath.section; //or any you like 
[cell.contentView addSubview:button]; //adding your button 

、その後、ボタンのコールバックで:

int row=0; 
int section=0; 
long tag = [sender superview].tag; 
NSLog(@"tag: %ld", tag); 
for (int i = 0; i<[data count]; i++) { 
    if (tag>=10000) 
    { 
     tag-=10000; 
     section++; 
    } 
    else i = [data count]; 
} 
row = tag; 

NSIndexPath *indexPath = [[NSIndexPath alloc] initWithIndex:section]; 
indexPath = [indexPath indexPathByAddingIndex:row]; 
[tableView cellForRowAtIndexPath:indexPath]; 

、あなたのセルへのフルアクセスを持っています。 rawsectionを計算すると、セルにアクセスできます。

+0

大丈夫、私はしました。助けてくれてありがとう!:D – iProRage

+0

私は私の答えをadited。これを試して。 – SentineL

+0

大丈夫です、これをsubtractLabelTextメソッドに入れますか?また、あなたのコードをコピーして貼り付けたところ、括弧の1つについて不平を言ってエラーが出てきています。私はそれを理解できません! :/ – iProRage

1

[sender superview]返しスーパー。

cellLabelをどのように構築して追加すれば、何が問題なのかを確認できます。

+0

大丈夫、助けてくれてありがとう!私は私の質問にそれを掲示するでしょう! – iProRage

関連する問題