将UITableViewCell中的UISwitch定位到右边

问题描述:

我可以知道如何将UITableViewCell中的UISwitch定位到正确的位置吗?我不使用故事板来添加切换按钮!将UITableViewCell中的UISwitch定位到右边

enter image description here

以下是在Controller.m或者


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

    NSString *note = [self.notes objectAtIndex:indexPath.row]; 
    [cell.textLabel setText:note]; 

    //TableView Cell Background Images 
    cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"bg.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]; 
    cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cell.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:5.0] ]; 

    //add switch button start 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    UISwitch *aSwitch = [[UISwitch alloc] init]; 
    [aSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; 
    [cell.contentView addSubview:aSwitch]; 
    //add switch button end 

    return cell; 
} 

+0

你有一个很常见的问题 - 细胞被重用。每次滚动时,重用的单元格都将获得另一个新滑块。您将继续为每个单元添加越来越多的开关。 – rmaddy

+0

是的,这是我想要的。每个单元都有自己的开关。 – AmirolAhmad

你可以试试这个....

UISwitch *aSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(225.0, 0.0, 80.0, 45.0)]; 
[aSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; 
aSwitch.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; 
[cell.contentView addSubview:aSwitch]; 
+0

非常感谢你..它完美的工作! – AmirolAhmad

设置帧(或中心)的代码。但同时,也要

  1. 创建,当你创建单元格添加开关,否则就会有多个交换机在细胞
  2. 配置可以确保它存在

喜欢的东西之后的电池:

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

    //add switch button start 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 

     //TableView Cell Background Images 
     cell.backgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"bg.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ]; 
     cell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"cell.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:5.0] ]; 

     cell.frame = CGRectMake(0, 0, 200, 44); 

     // Configure the cell... 
     UISwitch *aSwitch = [[UISwitch alloc] init]; 
     aSwitch.tag = 123123; 

     CGRect switchFrame = aSwitch.frame; 
     switchFrame.origin.x = 200 - switchFrame.size.width; 
     switchFrame.origin.y = 22 - (switchFrame.size.height * 0.5); 
     aSwitch.frame = switchFrame; 

     [aSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventValueChanged]; 
     [cell.contentView addSubview:aSwitch]; 
     //add switch button end 
    } 

    // configure the cell 
    NSString *note = [self.notes objectAtIndex:indexPath.row]; 
    [cell.textLabel setText:note]; 

    // configure the switch 
    UISwitch *aSwitch = (UISwitch *)[cell viewWithTag:123123]; 
    aSwitch. ...; 

    return cell; 
} 
+0

非常感谢您的帮助。我会尝试 – AmirolAhmad

或者您可以继承UITableCellView并添加UISwitch并在其中设置其框架。这样,您所要做的就是在cellForRowAtIndexPath中设置目标和操作。