Why do you try to allocate this cell every single time? You should dequeue it. I try this code and it works fine for me:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
UISwitch * cellSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(200, 10, 50, 50)];
[cell.contentView addSubview:cellSwitch];
return cell;
}
Just remember to set up cell identifier.
//EDITED
I would remove SwitchCellMake method and add UISwitch to the contentView in initWithStyle:reuseIdentifier method (or initWithCoder if you use nib):
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.cellSwitch = [[UISwitch alloc] init];
[self.contentView addSubview:self.cellSwitch];
}
return self;
}
As you can see cellSwitch is a property so you can set up the frame in layoutSubview (you can handle the orientation changing here):
-(void)layoutSubviews
{
[self.cellSwitch setFrame:CGRectMake(200, 10, 50, 50)];
}
After that just register your cell in storyboard or in viewDidLoad (if you don't use nib) and change your init method to:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
SwitchCell *cellSwitchCell = (SwitchCell *)[tableView dequeueReusableCellWithIdentifier:@"Cell"];
cellSwitchCell.textLabel.text = [NSString stringWithFormat:@"Row: %d", indexPath.row];
return cellSwitchCell;
}
This is how I would do this.