DTGridView will lose content when scrolling

I am using DTGridView from DTKit Daniel Tull. I implemented it in a very simple ViewController, and the test I do is put the button in the last row of the grid, which should add another row to the grid (and for that move the button in the row below it).

The problem is that when I click the button a few times and then start scrolling, the grid seems to be losing its content. Since I'm not completely sure that this is an error in the grid, but more in my code, I hope you guys can help me and track the error.

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

#import <UIKit/UIKit.h> #import "DTGridView.h" @interface TestController : UIViewController <DTGridViewDelegate, DTGridViewDataSource> { DTGridView* thumbGrid; } @end 

I declare DTGridView, which will be my grid, where I want to place the content.

Now my code file is:

 #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:@"%dx %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 a DataSource that seem to work. The grid is filled with so many lines as my "lines" (+1). The last row does NOT contain 3 columns, but only one. This cell contains a button that (when pressed) adds 1 to the integer "rows".

The problem begins when cell reuse begins (I guess), and the content begins to disappear. When I scroll through the backups, the UILabels that I put in the cells disappear.

Is there an error, code error, error, dumb-ass-move. Am I out here? Hope someone can help.

+4
source share
2 answers

I quickly looked through the code, and you should definitely call reloadData instead of setNeedsDisplay in selectLoadMoreItems like this:

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

Let me know if this fixes, if not, I will review your code later.

+2
source

Here 's a migration request that seems to solve this problem. The bottom line calls initialiseViews every time checkViews is called.

+1
source

Source: https://habr.com/ru/post/1308282/


All Articles