按钮添加为表视图中的子视图,也显示在其他单元格中



我有表格视图,在第二节的行中我放置了一个按钮,当我滚动表格视图时,这个按钮也会出现在其他行中。

- (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];
        cell.textLabel.text=@"Warm Up(5 min.)";
        [cell.accessoryView addSubview:self.warmUpSwitch];
    }
    if (indexPath.row==2)
    {
        cell.accessoryView=[[UIView alloc]initWithFrame:self.coolDownSwitch.frame];
        cell.textLabel.text=@"Cool Down(5 min.)";
        [cell.accessoryView addSubview:self.coolDownSwitch];
    }
}

试试这个,

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

相关内容

  • 没有找到相关文章

最新更新