ios - 将 UITableViewCell 中的 UISwitch 定位到右侧

标签 ios uitableview uiswitch

我可以知道如何将 UISwitch 在 UITableViewCell 中定位到正确的位置吗?我没有使用 Storyboard 添加切换按钮!

enter image description here

Here is the code in 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;
}

最佳答案

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

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

关于ios - 将 UITableViewCell 中的 UISwitch 定位到右侧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16844031/

相关文章:

iOS 透明 View Controller

ios - 在 Swift 4 中调整 UISwitch 的大小

ios - 为什么每次在UITableViewController内部以不同的顺序显示我的核心数据?

ios - 到达 UITableView 底部后加载更多

Android 相当于 UISwitch

ios - 没有获取用于在 UITableViewCell 中点击 UISwitch 的 indexPath

选择 TextView 时 ios Tableview 单元格消失

ios - 当我使用 Storyboard 创建我的第一个 CollectionView 时,CollectionViewCell 从顶部开始默认插入?

ios - 正确使用 Grand Central Dispatch 与多个 UIActivityIndi​​catorViews

ios - UITableView indexPath.row 不增加