将UISwitch定位在UITableViewCell中

时间:2013-05-30 19:09:05

标签: ios uitableview uiswitch

我可以知道如何将UISwitch定位在UITableViewCell中吗?我没有使用故事板来添加切换按钮!

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

3 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:0)

设置框架(或中心)。但是:

  1. 创建单元格时创建并添加开关,否则单元格中将有多个开关
  2. 确定单元格确定后,配置单元格
  3. 类似的东西:

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

答案 2 :(得分:0)

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