我可能知道如何将UITableViewCell中的UISwitch定位到正确的位置?我没有使用情节提要添加“切换按钮”!


这是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 - 将UISwitch在UITableViewCell中向右定位,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16844031/

10-09 01:45