0

我的应用程序有一个来自服务器的列表,每次滚动时我都会发送新数据请求并重新加载它。我接受了 10 个细胞。这工作就好了。

但问题是,如果我更改例如 1 个单元格中的值,它会在单元格 8、15 等中更改。

数据是类,我从服务器获取信息以显示在单元格中。getDataAt 是我的单元格类中的方法,它将值放入单元格中。

在 numberOfRowsInSection 中,我正在检查列表是否完整,表格视图显示附加单元格以加载更多行,如果列表不完整,则意味着没有更多行可显示并且此单元格将不会显示。

代码 :

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row < [self.list size])
     {

        static NSString *cellIdentifier = @"CellIdentifier";

        self.cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

        Data* data = [self.list getDataAt:indexPath.row];

        NSLog(@"%@", indexPath);
        cell.delegate = self;
        [cell setCell:data];


        return  _cell;

    }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  {

return [self.list size] + (self.list.complete ? 0 : 1);

}

到目前为止我发现的是,当我向上和向下滚动时,indexPath 会变得混乱,当他再添加 10 个单元格时,它需要 indexPath.row 以前的单元格。

4

3 回答 3

1
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {   
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)  
   {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    if (indexPath.row < [self.list size]) 
     {
        Data* data = [self.list getDataAt:indexPath.row];
        NSLog(@"%@", indexPath);
        [cell setCell:data];
     }

   return cell;
}
于 2013-03-14T12:54:21.497 回答
0

在大多数情况下,您不需要检查行索引是否超出范围 - 相反,您应该在 numberOfRowsInSection 方法中正确返回值。如果 (indexPath.row < [self.list size]) 错误,你会返回什么?如果你返回 nil - 它会崩溃。

此外,这可能是一个错误:您使用不同的变量来访问单元格:'cell' 和 '_cell'。并且不需要将最后一个单元格保存在局部变量中。

我假设您之前为表格视图注册了可重用单元格的类(或笔尖)。

尝试这样的事情:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"CellIdentifier";

    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.delegate = self;
    Data* data = [self.list getDataAt:indexPath.row];
    [cell setCell:data];
    return  cell;

}

– tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.list size];
}
于 2013-03-14T14:11:35.040 回答
0

主要问题是单元格分配效率不高。您需要以自己的方式实现以下内容:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier: cellIdentifier] autorelease];
    [self setCell:cell atIndex:indexPath.row];
    }
    else{
    [self reuseCell:cell atIndex:indexPath.row];
    }
    return cell;
}

setCell:atIndex: 方法如下:

setCell:(UITableViewCell*)cell atIndex:(int)index{
Data* data = [self.list getDataAt:index];
[cell setCell:data];
}

假设您的单元格上有一个标签,您将在 setCell 上的单元格上添加标签。

(void)setCell:(Data*)data{
label.text = @"text from data";
[cell addSubView:label];//this is the important part, you are adding the label on cell as a subview
//doing other stuff
}

现在,reuseCell 方法将类似于以下(假设您的单元格上有一个标签,此时您需要重置标签,而不是分配或添加标签到您的单元格):

reuseCell:(UITableViewCell*)cell atIndex:(int)index{
Data* data = [self.list getDataAt:index];
cell.label.text = @"text from data";//here your are modifying the label, not adding the label as a subView of the cell
//doing other stuff
}

最后,您无需检查“indexPath.row < [self.list size]”,因为您已经在说“- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger) 上的单元格总数”部分”。

如果您可以提供“setCell:”方法的详细信息以及为什么使用单元格作为属性,我会更清楚地描述它。

于 2013-04-29T07:31:56.787 回答