user3837816
user3837816

Reputation: 119

customTableViewCell not working

i've created a tableViewCell subclass called TestTableViewCell. i've connected datasource and delegate by using this. The problem is that it does not seem to do anything that i've stated in the subclass, not even changing the background?

self.tableView.delegate = self;
self.tableView.datasource = self;

i've changed the cell class to TestTableViewCell and created an identifier called Cell.

cellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[TestTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }




    return cell;
}

subclass

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.frame) - 60, 10, 50, 30)];
        [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn1 setTitle:@"B1" forState:UIControlStateNormal];
        [self.contentView addSubview:btn1];

       UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMinX(btn1.frame), 70, 50, 30)];
        [btn2 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn2 setTitle:@"B2" forState:UIControlStateNormal];
        [self.contentView addSubview:btn2];

        UIButton *btn3 = [[UIButton alloc] initWithFrame:CGRectMake(CGRectGetMinX(btn1.frame), 130, 50, 30)];
        [btn3 setTitle:@"B3" forState:UIControlStateNormal];
        [btn3 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.contentView addSubview:btn3];

        UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(moveCover:)];
        gesture.direction = UISwipeGestureRecognizerDirectionLeft;

        UISwipeGestureRecognizer *gesture2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(moveCover:)];
        gesture2.direction = UISwipeGestureRecognizerDirectionRight;

        _coverView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, CGRectGetWidth(self.frame), 195)];
        _coverView.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:_coverView];

        [self addGestureRecognizer:gesture];

        [self addGestureRecognizer:gesture2];

        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
   return self;
}

Upvotes: 0

Views: 51

Answers (1)

Wizkid
Wizkid

Reputation: 1035

Assuming you are using Interface Builder and have a UITableView with a 'custom' dynamic prototype cell. Did you assign your TestTableViewCell subclass to the table cell row in IB (like you do for UITableViewController when you create a custom UITableViewController subclass? If you don't do this then when you dequeue your cell, you will get the cell (and default class) as defined in IB, not your custom cell with custom class.

Also, note, that all of the button creation logic, etc. in your subclass could go directly into your custom cell (visually) within IB and you can avoid all that code if you so choose. Just visually design your row in IB then use Assistant Editor to create outlets and actions in your custom cell (TestTableViewCell) header and implementation (.m).

Finally, you don't need to allocate the cell in CellForRowAtIndexPath

Upvotes: 1

Related Questions