2013-02-22 89 views
1

我有一個問題,我有表視圖和第二節的行我已經放置了一個按鈕,當我滾動表視圖此按鈕也出現在其他行,那麼我應該如何解決這個問題?按鈕添加爲表視圖showin我其他單元格中的子視圖

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


    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];/////self...ramesh 
    if (cell == nil) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kCellID] ; 


    } 




     if (indexPath.row==1) 
     { 
      cell.accessoryView=[[UIView alloc]initWithFrame:self.warmUpSwitch.frame]; 
      [email protected]"Warm Up(5 min.)"; 
      [cell.accessoryView addSubview:self.warmUpSwitch]; 

     } 
     if (indexPath.row==2) 
     { 
      cell.accessoryView=[[UIView alloc]initWithFrame:self.coolDownSwitch.frame]; 

      [email protected]"Cool Down(5 min.)"; 
      [cell.accessoryView addSubview:self.coolDownSwitch]; 
     } 

} 
+2

你能告訴你的代碼,我懷疑你使用的是可重用的所有細胞,並獲得了不需要的細胞按鈕以及 – DivineDesert 2013-02-22 07:04:09

+0

Althought這不是一個好方法,但設置可重複使用的標識符到零將幫助你。 – rptwsthi 2013-02-22 07:13:52

+0

請發佈您的代碼如何添加按鈕 – Warewolf 2013-02-22 07:17:42

回答

0

試試這個,

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) 
{ 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease]; 


} 
    if (indexPath.row==1) 
    { 

    UIButton *btnwarmUpSwitch = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; 
    btnwarmUpSwitch.frame = CGRectMake(250,20,100,50); //set frame where you want to set 
    [btnwarmUpSwitch setTitle:@"Warm Up(5 min.)" forState:UIControlStateNormal]; 
    btnwarmUpSwitch.tag=indexPath.row; 
    [btnwarmUpSwitch addTarget:self action:@selector(selectwarmUpSwitch:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.contentView addSubview:btnwarmUpSwitch]; 


    } 
    if (indexPath.row==2) 
    { 
     UIButton *btncoolDownSwitch = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain]; 
    btncoolDownSwitch.frame = CGRectMake(250,20,100,50); //set frame where you want to set 
    [btncoolDownSwitch setTitle:@"Cool Down(5 min.)" forState:UIControlStateNormal]; 
    btncoolDownSwitch.tag=indexPath.row; 
    [btncoolDownSwitch addTarget:self action:@selector(selectcoolDownSwitch:) forControlEvents:UIControlEventTouchUpInside]; 
    [cell.contentView addSubview:btncoolDownSwitch]; 
    } 
    return cell; 
} 
- (void) selectwarmUpSwitch:(id)sender 
{ 

} 
- (void) selectcoolDownSwitch:(id)sender 
{ 

} 
+0

我做的改變,但這個問題的超文本顯示字段和切換文本fiels stilll.please告訴我使用標記的好處,意味着textfield.tag。 – RameshRajput 2013-02-25 07:19:53

+0

得到一個特定的文本字段指針值 – Ravindhiran 2013-02-25 07:43:06

+0

還有一個條件也可能有幫助,我已經添加了表視圖作爲子視圖,並在其上添加了按鈕和文本字段,可能是這樣的事情沒有清除單元格的可重用性 – RameshRajput 2013-02-27 04:02:04

相關問題