开发者

DTGridView losing content while scrolling

开发者 https://www.devze.com 2022-12-29 01:09 出处:网络
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

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 allo开发者_运维知识库c] 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.


I've had a quick look at the code and you should definitely be calling reloadData instead of setNeedsDisplay in selectLoadMoreItems like so:

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

Let me know if this fixes it, if not I'll take a proper look through your code later today.


There's a pull request that seems to solve this issue. The bottom line is calling initialiseViews everytime checkViews is called.

0

精彩评论

暂无评论...
验证码 换一张
取 消