Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/44.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone UITableView数据在滚动时显示多次_Iphone_Ios_Objective C_Uitableview - Fatal编程技术网

Iphone UITableView数据在滚动时显示多次

Iphone UITableView数据在滚动时显示多次,iphone,ios,objective-c,uitableview,Iphone,Ios,Objective C,Uitableview,我有一个UITableView,其中显示数据,但当它滚动数据(UILabel)时,数据要么消失,要么一次又一次地添加到彼此的顶部。每次如果我滚动每个单元格交换数据 这是我的cellForRowAtIndexPath:code -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cell

我有一个
UITableView
,其中显示数据,但当它滚动数据(
UILabel
)时,数据要么消失,要么一次又一次地添加到彼此的顶部。每次如果我滚动每个单元格交换数据

这是我的
cellForRowAtIndexPath:
code

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

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if(cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        }

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        // configure the cell's background
        UIImageView *gradient = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradient"]];
        [cell.contentView addSubview:gradient];

        // configure the cell's label
        UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 130, 300, 44)];

        // grab a reference to the label's text from the tableData
        nameLabel.text = [name objectAtIndex:indexPath.row];
        nameLabel.textColor = [UIColor blackColor];
        nameLabel.font = [UIFont fontWithName:@"DIN-Bold" size:12];
        nameLabel.backgroundColor = [UIColor clearColor];

        // set the autoReiszing mask -- this way if the label spills over the editing
        // [icon?] then the text will trail off in ...
        nameLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;

        [cell.contentView addSubview:nameLabel];

        // configure the cell's label
        UILabel *tableTextViewLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 80, 220, 50)];

        // grab a reference to the label's text from the tableData
        tableTextViewLbl.text = [message objectAtIndex:indexPath.row];
        tableTextViewLbl.textColor = [UIColor blackColor];
        tableTextViewLbl.font = [UIFont fontWithName:@"DIN" size:10];
        tableTextViewLbl.backgroundColor = [UIColor clearColor];
        tableTextViewLbl.autoresizingMask = UIViewAutoresizingFlexibleWidth;

        [cell.contentView addSubview:tableTextViewLbl];

        // configure the cell's label
        UILabel *tableTimeStampViewLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 30, 200, 50)];

        // grab a reference to the label's text from the tableData
        tableTimeStampViewLbl.text = [timeStamp objectAtIndex:indexPath.row];
        tableTimeStampViewLbl.textColor = [UIColor lightGrayColor];
        tableTimeStampViewLbl.font = [UIFont fontWithName:@"DIN" size:7];
        tableTimeStampViewLbl.backgroundColor = [UIColor clearColor];
        tableTimeStampViewLbl.autoresizingMask = UIViewAutoresizingFlexibleWidth;

        [cell.contentView addSubview:tableTimeStampViewLbl];

    //   UIImageView *image;
    //   UIImage *image1=[UIImage imageNamed:@"rest.png"];
    //   image=[[UIImageView alloc]initWithImage:image1];
    //   image.frame=CGRectMake(10,30,40,30);
    //   
    //   [cell.contentView addSubview:image];
    //


        return cell;

    }

每次将单元格加载/重新加载到视图中时,都会创建UILabel实例。事情不是这样的。相反-在UITableView子类中将UILabel添加为属性(可能是IBOutlet),并在
cellForRowAtIndexPath:
中对其进行更改

因此,您将有一个新类,继承自UITableViewCell——我们称之为MyCustomCell

在MyCustomCell.h中:

@interface MyCustomCell : UITableViewCell

@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

@end
MyCustomCell.xib将定义UILabel位置和配置,当然需要与nameLabel属性关联

在CellForRowatineXpath中,您只需参考cell.namelab并更改文本,而不是实例化新的UILabel。请确保在interface builder中为单元类定义resuseIdentifier,并使用以下命令对其进行实例化:

MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

if (!cell) {
    cell = [[MyCustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIdentifier"];

}
这样

if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
else{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

但选择定制手机是最好的答案。检查此链接为

提供最佳解决方案在您遵循我的答案之前,我想告诉您,以下代码对内存管理不利,因为它将为
UITableView
的每行创建新的单元格,所以请小心

但是最好使用,当
UITableView
的行数有限时(大约50-100行),那么下面的代码对您的情况很有帮助,如果适合您,请使用它

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

    NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

         /// Put your code here
     }

    return cell;
}

如果行数有限,则这是最适合您的代码。

首先,您需要了解UITableView的工作原理。实际上,UItableView单元格的概念是,每次滚动表视图时,它不是为您创建新单元格,而是在cellIdentifier的帮助下重用单元格

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

    NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

         /// Put your code here
     }

    return cell;
}
UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier]
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *cellIdentifier = @"Cell";

    UILabel *nameLabel = nil;

    UILabel *tableTextViewLbl= nil;

    UILabel *tableTimeStampViewLbl= nil;
    //Here we are , reuse the cells...
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];



    if(cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

        // configure the cell's background
        UIImageView *gradient = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gradient"]];
        [cell.contentView addSubview:gradient];

        // configure the cell's label
        nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 130, 300, 44)];

        // grab a reference to the label's text from the tableData
        nameLabel.textColor = [UIColor blackColor];
        nameLabel.font = [UIFont fontWithName:@"DIN-Bold" size:12];
        nameLabel.backgroundColor = [UIColor clearColor];
        nameLabel.tag = 111;//Giving this component to tag so we can access it

        // set the autoReiszing mask -- this way if the label spills over the editing
        // [icon?] then the text will trail off in ...
        nameLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth;

        [cell.contentView addSubview:nameLabel];

        // configure the cell's label
        tableTextViewLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 80, 220, 50)];
        tableTextViewLbl.textColor = [UIColor blackColor];
        tableTextViewLbl.font = [UIFont fontWithName:@"DIN" size:10];
        tableTextViewLbl.backgroundColor = [UIColor clearColor];
        tableTextViewLbl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        tableTextViewLbl.tag = 222;//Giving this component to tag so we can access it
        [cell.contentView addSubview:tableTextViewLbl];

        // configure the cell's label
        tableTimeStampViewLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 30, 200, 50)];
        tableTimeStampViewLbl.textColor = [UIColor lightGrayColor];
        tableTimeStampViewLbl.font = [UIFont fontWithName:@"DIN" size:7];
        tableTimeStampViewLbl.backgroundColor = [UIColor clearColor];
        tableTimeStampViewLbl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        tableTimeStampViewLbl.tag = 333;//Giving this component to tag so we can access it
        [cell.contentView addSubview:tableTimeStampViewLbl];
    }



    nameLabel = (UILabel*)[cell.contentView viewWithTag:111];//Here we access the name label with the help of tag, is that we have assigned tag while making the componant.
    nameLabel.text = [name objectAtIndex:indexPath.row];


    tableTextViewLbl = (UILabel*)[cell.contentView viewWithTag:222];//Here we access the name label with the help of tag, is that we have assigned tag while making the componant.
    // grab a reference to the label's text from the tableData
    tableTextViewLbl.text = [message objectAtIndex:indexPath.row];


    tableTimeStampViewLbl = (UILabel*)[cell.contentView viewWithTag:333];//Here we access the name label with the help of tag, is that we have assigned tag while making the componant.
    // grab a reference to the label's text from the tableData
    tableTimeStampViewLbl.text = [timeStamp objectAtIndex:indexPath.row];


    return cell;

}

只需添加以下代码

NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews];
for (UILabel *subview in subviews)
{
     [subview removeFromSuperview];
}
[subviews release];
subviews = nil; 
在-

if (cell == nil)
{
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault        reuseIdentifier:CellIdentifier];
} 

然后添加您的代码。

@user2588945您检查过我的答案了吗?是的,这行不通。他还是会遇到同样的问题。此外,你所做的毫无意义。if和else语句的作用完全相同。(顺便说一句,我并没有否决投票)。好吧,但这件事对我来说是可行的,在某些情况下它可能不起作用。当然,它也可能导致内存相关的崩溃。这段代码没有任何作用。您有一个冗余的if/else块。在这方面,你没有做任何有用的事情,OP可以用来阻止他有多个标签的问题。事实上,您的代码与问题中的问题代码相同。这没有任何补充。不,请不要接受这个答案。在这种情况下,这可能是你能做的最糟糕的事情。您几乎肯定会有内存问题,并且表的工作速度会非常慢。请投反对票。使用@Stavash answer,它是正确的,可以解决您的问题。没问题。每当您准备好使用UITableView执行更复杂的操作时,请尝试阅读以下内容: