2016-03-23 17 views
0

テーブルのセル数は1で、各セルのセル数はUIButtonです。ボタンをクリックすると、ボタンのセルの背景色が変わります。ここにあなたの理解のための私の部分的なコードです。iOSの特定のインデックスセルの背景色を変更します。

このボタンをクリックすると、すべてのテーブルビューのセルの背景色が変更されます。どのように選択したセルのためにそれを行うには?

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

    cell = (MenuCell *)[menuTableview dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) { 
     NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MenuViewCell" owner:self options:nil]; 
     cell = [nib objectAtIndex:0]; 

     [cell initState]; 

     // Listens for method calls on cell 
     cell.delegate = self; 
    } 

    if ((self.TempArray.count != 0)) { 
     for (int i=0; i<TempArray.count; i++) { 
      if ([self.TableviewArray objectAtIndex:indexPath.row] == TempArray[i]) { 
       if (cell.backgroundColor != [UIColor lightGrayColor]) { 
        cell.backgroundColor = [UIColor lightGrayColor]; 
       } 

       UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect]; 
       button.tag=indexPath.row; 
       [button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchDown]; 
       [button setTitle:@"UnHide" forState:UIControlStateNormal]; 
       button.frame = CGRectMake(200.0, 20.0, 160.0, 40.0); 
       [cell.contentView addSubview:button]; 
      } 
     } 
    } 

    // Set index for this cell (it could be wrong if cell is re-used) 
    cell.downloadIndex = (int)indexPath.row; 

    Download *download = [TableviewArray objectAtIndex:indexPath.row]; 

    cell.downloading = download.downloading; 
    cell.completed = download.completed; 
    cell.MenuLang.text = download.name; 
    cell.downLoadPrgView.progress = download.percentageDownloaded; 

    // Check for completed status 
    cell.completed = download.completed; 
    cell.CompletedMenuLang.text = download.name; 

    return cell; 
} 

-(void)aMethod:(UIButton*)sender { 
    NSLog(@"I Clicked a button %d",sender.tag); 
    int row = sender.tag; 
} 
+0

を試してみてください。あなたのカスタムセルの背景が透明な色であることを確認してください –

+0

私はそれがセルのために起こった..私はすべての色を変更したくない..私はちょうどボタンが選択されている特定のセルの色を変更する必要がある – RajaSasidharan

+0

特定のセルの他のセルの色が変更されました.. – RajaSasidharan

答えて

0

は、単にコードの下に実装するには、バックグラウンドの特定のセルの色などを変更することができます選択したボタンを非表示にします。

-(void)aMethod:(UIButton*)sender { 
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:sender.tag inSection:0]; 
Friends1TableViewCell *cell = (Friends1TableViewCell *)[self.tblVW cellForRowAtIndexPath:indexPath]; 
cell.backgroundColor = [UIColor redColor]; 
sender.hidden = YES; 
} 
+0

私はテーブルビューをスクロールすると、赤色が保持されますか、保持されません。 –

+0

ありがとうございます。それは正常に動作します – RajaSasidharan

0
-(IBAction)BTNTapped:(UIButton *)sender 
    { 
    CGPoint buttonPosition = [sender convertPoint:CGPointZero 
              toView: menuTableview]; 
    NSIndexPath *tappedIP = [menuTableview indexPathForRowAtPoint:buttonPosition]; 

} 

ここでセル

+0

Thanks dude。私は答えを得た。特定のセルで同じクリックされたボタンを非表示にする必要があります。私はそれのためにどうすればいいですか? – RajaSasidharan

+0

uを取得できません。ボタンが選択されているボタンを非表示にしたい –

+0

すべてのセルにボタンがあります。ボタンをクリックすると、ボタンが非表示になり、セルの背景色が変わるはずです – RajaSasidharan

0

のこのindexpathを使用して変更できます私の答え..です

-(void)aMethod:(UIButton*)sender 
{ 
    NSLog(@"I Clicked a button %d",sender.tag); 



    int row = sender.tag; 

    NSIndexPath *path = [NSIndexPath indexPathForRow:row inSection:0]; 


    UITableViewCell *cell = [menuTableview cellForRowAtIndexPath:path]; 
    cell.contentView.backgroundColor = [UIColor whiteColor]; 
} 
+0

という質問が1つあります。テーブルビューをスクロールすると、ホワイトカラーは –

+0

のままになりません。デキューを使用しているため、配列にthtのインデックスパスを格納する必要があります。配列にindexpathが含まれている場合は、indexapthの行のセルでチェックしてから、次に白い、それ以外の場合は、何をしたいのですか? –

+0

Asad ali。私はあなたが投稿した上記の答えを試しました。それはうまく働いています – RajaSasidharan

0

[UIColor lightGrayColor] = cell.contentview.backgroundを試してみてください。この

@property (nonatomic, assign) NSInteger selectedRow; 

self.selectedRow = -1;   // init with -1 


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

    // ... 

    if(self.selectedRow == indexPath.row) { 
     cell.backgroundColor = SELECTED_COLOR; 
    } 
    else { 
     cell.backgroundColor = NORMAL_COLOR; 
    } 

    return cell; 
} 

-(void)aMethod:(UIButton*)sender { 

    // ... 

    self.selectedRow = row; 
    [self.myTableView reloadData]; 
} 
関連する問題