Frage

i have made a normal UISwitch programmatically in objective c inside a UITableViewCell and when i go to view it in the simulator i see this:

Graphics Glitch

Why is it happening and how can i fix it?

Here is the code for how i am implementing it:

UISwitch Class:

#import <Foundation/Foundation.h>

@interface SwitchCell : UITableViewCell
+ (SwitchCell*)SwitchCellMake;
@end


@implementation SwitchCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
    }
    return self;
}

+ (SwitchCell*)SwitchCellMake{
    SwitchCell * newSwitchCell = [[SwitchCell alloc]init];

    UISwitch * cellSwitch = [[UISwitch alloc] init];
    [newSwitchCell.contentView addSubview:cellSwitch];
    [cellSwitch setCenter:CGPointMake(600, 30)];

    return newSwitchCell;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
    [super setSelected:selected animated:animated];
}

@end

ViewDidLoad:

- (void)viewDidLoad{
    [super viewDidLoad];
    [arySwitchCells addObject:[SwitchCell SwitchCellMake]];
}

And the cellForRowAtIndexPath method:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SwitchCell *cellSwitchCell = (SwitchCell *)[tableView dequeueReusableCellWithIdentifier:@"SwitchCell"];
    cellSwitchCell = [arySwitchCells objectAtIndex:indexPath.row];
    return cellSwitchCell;
}
War es hilfreich?

Lösung

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top