iphone - DTGridView 在滚动时丢失内容

标签 iphone objective-c cocoa-touch

我正在使用 DTKit 中的 DTGridView由丹尼尔图尔。我在一个非常简单的 ViewController 中实现了它,我正在做的测试是在网格的最后一行放置一个按钮,这应该向网格添加另一行(因此将按钮移动到它下面的一行)。

问题是,当我点击按钮几次然后开始滚动时,网格似乎丢失了它的内容。由于我不能完全确定这是网格中的错误,但更多的是在我的代码中,我希望你们能帮助我找出错误。

首先我有我的头文件,它很简单,因为这是一个测试:

#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

我实现了 DataSource 的所有方法,这些方法似乎有效。网格中充满了与我的 int 'rows' ( +1 ) 一样多的行。最后一行不包含 3 列,而只有一列。该单元格包含一个按钮(按下时)将“行”整数加 1。

当它开始重用单元格(我猜)并且内容开始消失时,问题就开始了。当我向上滚动时,我放入单元格中的 UILabel 消失了。

我在这里遗漏了一些错误、代码错误、错误、愚蠢的举动吗?希望任何人都可以提供帮助。

最佳答案

我快速查看了代码,您绝对应该在 selectLoadMoreItems 中调用 reloadData 而不是 setNeedsDisplay,如下所示:

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

如果这解决了问题,请告诉我,如果不能,我会在今天晚些时候仔细查看您的代码。

关于iphone - DTGridView 在滚动时丢失内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2732602/

相关文章:

c# - 如何在 Xamarin.iOS 中包装 Objective-C 库?

iphone - View Controller 上的 TableView Controller

iphone - 如何检测主机设备是iPhone还是iPad?

iphone - UITableView 显示键值对

iphone - 获取图像索引

iOS 模拟器不为 iPhone 旋转(适用于 iPad)

objective-c - 从动态单元格中的文本字段获取数据时出现问题

cocoa-touch - 触摸时不突出显示 TableView 行

ios - 如何在 Swift 中以编程方式为 UILabel 提供动态高度?

iphone - 将数组的内容写入文本文件