0

我有一个UITableView数据显示的位置,但是当它滚动数据时(UILabel)要么消失,要么它们被一遍又一遍地添加到彼此之上。每次如果我滚动每个单元格交换数据。

这是我的cellForRowAtIndexPath:代码

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

    }
4

5 回答 5

3

每次将单元格加载/重新加载到视图中时,您都会创建一个 UILabel 实例。这不是它的完成方式。相反 - 在 UITableView 子类中将 UILabel 作为属性(可能是 IBOutlet)添加并在cellForRowAtIndexPath:.

所以你会有一个新的类,继承自 UITableViewCell - 我们称之为 MyCustomCell。

在 MyCustomCell.h 中:

@interface MyCustomCell : UITableViewCell

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

@end

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

在 cellForRowAtIndexPath 中,您只需引用 cell.nameLabel 并更改文本,而不是实例化新的 UILabel。请务必在接口生成器中为单元类定义一个 resuseIdentifier 并使用以下方法对其进行实例化:

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

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

}
于 2013-08-05T10:37:47.367 回答
1

只需添加以下代码

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

然后添加您的代码。

于 2013-08-05T11:10:53.760 回答
0

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

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;

}
于 2013-08-05T10:55:06.233 回答
0

在您遵循我的答案之前,我想告诉您以下代码对内存管理不利,因为它会为 的每一行创建新单元格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;
}

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

于 2013-08-05T10:50:17.540 回答
-2

使用这种方式

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

但采用自定义单元格是最好的答案。检查此链接为自定义单元格提供了最佳解决方案

于 2013-08-05T10:46:44.337 回答