【问题标题】:Positioning UISwitch in UITableViewCell to right将 UITableViewCell 中的 UISwitch 定位到右侧
【发布时间】:2013-05-30 19:09:05
【问题描述】:

我可以知道如何将 UITableViewCell 中的 UISwitch 定位到右侧吗?我没有使用情节提要添加 Switch Button!

这是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;
}

【问题讨论】:

  • 您有一个非常常见的问题 - 单元格被重复使用。每次滚动时,重复使用的单元格都会获得另一个新滑块。您将不断向每个单元格添加越来越多的开关。
  • 是的,这就是我想要的。每个单元都有自己的开关。

标签: ios uitableview uiswitch


【解决方案1】:

你可以试试这个......

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];

【讨论】:

    【解决方案2】:

    设置框架(或中心)。而且:

    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;
    }
    

    【讨论】:

    • 非常感谢您的帮助。我会试试的
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-21
      相关资源
      最近更新 更多