0

I have UITableView with custom UITableViewCells. The Cells contain a custom view, which is a slightly modified version of SSCheckBoxView (available on GitHub)

When I scroll within the TableView and a cells disappears from the screen, the custom view is cut off when it reappears.

Before scrolling my cells look like this: https://www.dropbox.com/s/hcyvgumhr7t1fxg/before.png

And this is what the view looks like when it reappears: https://www.dropbox.com/s/p6wirtj60ffssxm/after.png

Here is my cellForRowAtIndexPath Method in my TableViewController

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *myIdentifier = @"CarpartTableViewCell";

    CarpartTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myIdentifier];

    if (cell == nil) {
        cell = (CarpartTableViewCell*) [[[NSBundle mainBundle] loadNibNamed:@"CarpartTableViewCell" 
                                                           owner:self 
                                                         options:nil] objectAtIndex:0];
    }

    cell.delegate = self;
    cell.tag = indexPath.row;

    Carpart *part = [_parts objectAtIndex:indexPath.row];

    cell.carpart.text = part.name;

    [cell.checkboxMaterial setText:@"Alubauteil"];
    [cell.checkboxPushing setText:@"Vordrücken"];

    cell.checkboxMaterial.checked = part.material.boolValue;
    cell.checkboxPushing.checked = part.pushing.boolValue;

    cell.checkboxPushing.tag = indexPath.row;
    cell.checkboxMaterial.tag = indexPath.row;

    [cell.checkboxMaterial setStateChangedTarget:self selector:@selector(materialChanged:)];
    [cell.checkboxPushing setStateChangedTarget:self selector:@selector(pushingChanged:)];

    return cell;
}
Daniel H.
  • 243
  • 1
  • 3
  • 10
  • You have at least one defect in your code - fix it first then try again. You are using an identifier which is a local variable to the method - change it to be "static NSString *myIdentifier = @"PartTableCellIdentifier";, try again, then update your description. Note the identifier ADDRESS is what is important, not the value. I usually use static "C" strings to use less memory. – David H Jul 16 '12 at 15:31
  • Updated my code to reflect the change (use of static identifier), but the problem still persists – Daniel H. Jul 16 '12 at 17:13
  • @DavidH this is a misnomer. All NSStrings initialized with @"" syntax are immutable and implicitly static, and compiler optimizes them accordingly. i.e. any two variables in your entire application which are initialized with @"foo" have the same exact value (i.e. they point to the same place in memory). – Chris Trahey Jul 16 '12 at 17:17
  • To clarify, the string they point to is static; the variable itself is not (but that doesn't really matter in this case).Anytime it points to a static string "literal" with the same value, it will point to the same place. – Chris Trahey Jul 16 '12 at 17:23
  • So you have these properties on your custom cell - checkboxMaterial etc - so how did you place them in your UITableViewCell? Are they contained within the "contentView" as subviews? If the answer is yes then you could write a little function to print out the frames of every contentView subview in the delegate method "willDisplay." That said, if you put these in the backgroundView (which you should not) then perhaps the labelView is overlaying a white background on those fields. – David H Jul 16 '12 at 17:41
  • Regarding the use of static - why make someone have to really think about it - all Apple code I've seen uses static. That way if you have two cells in a table, and two places that set the reuse identifier, and it so happens each person chose NSString *str = @"cell", and thus both user the same value, you don't spend hours or days tracking it down. Personally a believer in the make it clear and simple method of coding, and use consistent practices. – David H Jul 16 '12 at 17:44
  • My custom view is a direct subview of the TableViewCell. Its connected from the IB via IBOutlet. Posting the subview frames didn't help me, the frames size was the same all the time. – Daniel H. Jul 16 '12 at 18:18
  • You did not answer the question of WHERE the subviews get added. They SHOULD be added to the cell contentView, not the background view and not as subviews of the cell itself. – David H Jul 16 '12 at 19:52
  • Ok let me clarify this, they are in fact subviews of the cells contentView and not direct subviews of the cell. – Daniel H. Jul 16 '12 at 21:38
  • Well, if you want to post your custom cell class on the web somewhere, I'd be glad to look it over and even try to test with it. As with all things, there IS a reason for this behavior. – David H Jul 16 '12 at 21:44
  • This is the code for the TableViewCell and the custom control its holding: https://dl.dropbox.com/u/5910713/tableviewcell.zip What I was wondering about. Only the custom control is cut off, and everything else is fine. Is there a possibility that there is an error within the custom view? – Daniel H. Jul 16 '12 at 22:06
  • Ok I found the error. There was a UIView with a white background placed outside the bounds of the cell, that got pushed pack in when scrolling. – Daniel H. Jul 17 '12 at 10:28

0 Answers0