我使用DTGridView从DTKit由丹尼尔·塔尔。我在一个非常简单的ViewController中实现了它,并且我正在做的测试是在网格的最后一行放置一个按钮,该按钮应该向网格添加另一行(并将按钮移动到其下的一行)。DTGridView失去内容,同时滚动
的问题是,当我点击按钮几次,然后开始滚动,网格似乎失去其内容。由于我不完全确定这是网格中的一个错误,但更多的是在我的代码中,我希望你们可以帮助我并追踪错误。
首先,我有我的头文件,它是非常简单的,因为这是一个试验:
#import <UIKit/UIKit.h>
#import "DTGridView.h"
@interface TestController : UIViewController <DTGridViewDelegate, DTGridViewDataSource>
{
DTGridView* thumbGrid;
}
@end
我宣布一个DTGridView,这将是我的网格,在这里我希望把内容
现在,我的代码文件:
#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
我实现所有的数据源的方法,这似乎工作。网格中充满了我的int'rows'(+1)所具有的行数。最后一行不包含3列,但只有一列。该单元格包含一个按钮(按下时)将'rows'整数加1。
的问题开始,当它开始重用细胞(我猜测)和内容开始消失。当我向后滚动时,我放入单元格的UILabels消失了。
有一些bug,代码错误,错误,哑屁股的举动,我在这里失踪?希望任何人都能帮忙。
丹尼尔您好,感谢响应。我尝试过这样做(这是我的第一次尝试),但这会导致网格闪烁并且不能解决问题。 – 2010-04-29 09:27:20
我有同样的问题,并且reloadData不起作用,要么:( – 2010-12-22 15:06:35