2013-02-22 88 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

相关问题