DTGridView losting content while scrolling

Posted by Wim Haanstra on Stack Overflow See other posts from Stack Overflow or by Wim Haanstra
Published on 2010-04-28T19:39:04Z Indexed on 2010/04/28 20:17 UTC
Read the original article Hit count: 316

I am using DTGridView from the DTKit by Daniel Tull. I implemented it in a very simple ViewController and the test I am doing is to place a button in the last row of the grid, which should add another row to the grid (and therefor moving the button to a row beneath it).

The problem is, when I click the button a couple of times and then start scrolling, the grid seems to lose its content. As I am not completly sure this is a bug in the grid, but more in my code, I hope you guys can help me out and track down the bug.

First I have my header file, which is quite simple, because this is a test:

#import <UIKit/UIKit.h>
#import "DTGridView.h"

@interface TestController : UIViewController <DTGridViewDelegate, DTGridViewDataSource> 
{
    DTGridView*     thumbGrid;
}

@end

I declare a DTGridView, which will be my grid, where I want to put content in.

Now, my code file:

#import "TestController.h"

@implementation TestController

int rows = 1;

- (NSInteger)numberOfRowsInGridView:(DTGridView *)gridView
{
    return rows;
}

- (NSInteger)numberOfColumnsInGridView:(DTGridView *)gridView forRowWithIndex:(NSInteger)index
{
    if (index == rows - 1)
        return 1;
    else
        return 3;
}

- (CGFloat)gridView:(DTGridView *)gridView heightForRow:(NSInteger)rowIndex
{
    return 57.0f;
}

- (CGFloat)gridView:(DTGridView *)gridView widthForCellAtRow:(NSInteger)rowIndex column:(NSInteger)columnIndex
{
    if (rowIndex == rows - 1)
        return 320.0f;
    else
        return 106.6f;
}

- (DTGridViewCell *)gridView:(DTGridView *)gridView viewForRow:(NSInteger)rowIndex column:(NSInteger)columnIndex
{
    DTGridViewCell *view = [[gridView dequeueReusableCellWithIdentifier:@"thumbcell"] retain];

    if (!view)
        view = [[DTGridViewCell alloc] initWithReuseIdentifier:@"thumbcell"];

    if (rowIndex == rows - 1)
    {
        UIButton* btnLoadMoreItem = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, 301, 57)];
        [btnLoadMoreItem setTitle:[NSString stringWithFormat:@"Button %d", rowIndex] forState:UIControlStateNormal];
        [btnLoadMoreItem.titleLabel setFont:[UIFont boldSystemFontOfSize:20]];
        [btnLoadMoreItem setBackgroundImage:[[UIImage imageNamed:@"big-green-button.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
        [btnLoadMoreItem addTarget:self action:@selector(selectLoadMoreItems:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btnLoadMoreItem];
        [btnLoadMoreItem release];
    }
    else {
        UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(10,0,100,57)];
        label.text = [NSString stringWithFormat:@"%d x %d", rowIndex, columnIndex];     
        [view addSubview:label];
        [label release];
    }

    return [view autorelease];
}

- (void) selectLoadMoreItems:(id) sender
{
    rows++;
    [thumbGrid setNeedsDisplay];
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    thumbGrid = [[DTGridView alloc] initWithFrame:CGRectMake(0,0, 320, 320)];
    thumbGrid.dataSource = self;
    thumbGrid.gridDelegate = self;
    [self.view addSubview:thumbGrid];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)dealloc {
    [super dealloc];
}

@end

I implement all the methods for the DataSource, which seem to work. The grid is filled with as many rows as my int 'rows' ( +1 ) has. The last row does NOT contain 3 columns, but just one. That cell contains a button which (when pressed) adds 1 to the 'rows' integer.

The problem starts, when it starts reusing cells (I am guessing) and content start disappearing. When I scroll back up, the UILabels I am putting in the cells are gone.

Is there some bug, code error, mistake, dumb-ass-move I am missing here? Hope anyone can help.

© Stack Overflow or respective owner

Related posts about dtgridview

Related posts about iphone-sdk