iphone上表格视图中单元格的奇怪行为,如果其中一个导致另一个发生变化,则会发生变化

iphone上表格视图中单元格的奇怪行为,如果其中一个导致另一个发生变化,则会发生变化,iphone,ios,objective-c,xcode,uitableview,Iphone,Ios,Objective C,Xcode,Uitableview,我的应用程序有一个来自服务器的列表,每次滚动下降时,我都会发送新数据请求并重新加载。我收到10个细胞。这工作做得很好 但问题是,如果我改变值,例如在1个单元格中,它在单元格8、15中改变,依此类推 Data是一个类,在这个类中,我从服务器获取信息以显示在单元格中。 getDataAt是我的cell类中的方法,它将值放入cell中 在NumberOfRowsInIM检查列表是否完整部分中,表格视图显示附加单元格以加载更多行,如果列表不完整,则表示没有更多行可显示,并且此单元格将不显示 代码:

我的应用程序有一个来自服务器的列表,每次滚动下降时,我都会发送新数据请求并重新加载。我收到10个细胞。这工作做得很好

但问题是,如果我改变值,例如在1个单元格中,它在单元格8、15中改变,依此类推

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

在NumberOfRowsInIM检查列表是否完整部分中,表格视图显示附加单元格以加载更多行,如果列表不完整,则表示没有更多行可显示,并且此单元格将不显示

代码:

    - (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);

}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
if(indexath.row<[self.list size])
{
静态NSString*cellIdentifier=@“cellIdentifier”;
self.cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
Data*Data=[self.list getDataAt:indexPath.row];
NSLog(@“%@”,indexath);
cell.delegate=self;
[单元设置单元:数据];
返回单元;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
返回[self.list size]+(self.list.complete?0:1);
}
到目前为止,我发现当我上下滚动时,indexPath会变得混乱,当他再添加10个单元格时,它会占用前面单元格的indexPath.row。

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
- (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;
}
{ 静态NSString*CellIdentifier=@“Cell”; UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 如果(单元格==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1重用标识符:CellIdentifier]; } if(indexath.row<[self.list size]) { Data*Data=[self.list getDataAt:indexPath.row]; NSLog(@“%@”,indexath); [单元设置单元:数据]; } 返回单元; }
在大多数情况下,您不需要检查行索引是否超出范围-相反,您应该在numberOfRowsInSection方法中正确返回值。 如果它(indexPath.row<[self.list size])错误,您将返回什么?如果您返回nil,它将崩溃

此外,此处可能存在错误: 您正在使用不同的变量访问单元格:“cell”和“\u cell”。 并且不需要在局部变量中保存最后一个单元格

我假设您在前面为表视图注册了可重用单元的类(或nib)

试着这样做:

- (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];
}

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

-(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方法将与以下类似(假设您的单元格上有标签,此时您需要重置标签,而不是将标签分配或添加到单元格):

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


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

显示numberofrowsinSection的代码。如何更改单元格中数据的值?我有按钮加号和减号,以及显示当前值的标签-(NSInteger)tableView:(UITableView*)tableView numberofrowsinSection:(NSInteger)部分{return[self.articleList size]+(self.articleList.complete?0:1);}我正在做,是的,我在viewDidLoad中注册了-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)部分{return[self.articleList size]+(self.articlist.complete?0:1);}这是我的代码,我正在检查列表的页面大小是否为10,如果是,我添加了额外的单元格,负责加载更多的行,如果小于10,这个单元格将不显示
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
}