【问题标题】:UITableviewcell cell.textlabel.text repeats when scrolling the tableview? how to avoid the cell.textlabel.text repetation?滚动tableview时UITableviewcell cell.textlabel.text重复?如何避免 cell.textlabel.text 重复?
【发布时间】:2015-10-13 06:53:59
【问题描述】:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    cell.selectionStyle = UITableViewCellSelectionStyleBlue;

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

    cell.textLabel.textColor=[UIColor blackColor];
    cell.textLabel.font=[UIFont fontWithName:@"Helvetica Neue" size:14.0];


    if (indexPath.section == 0) {
    switch (indexPath.row) {
        case 0:
            cell.textLabel.text=@"Sub-Scheme 1";
              break;

        case 1:
            cell.textLabel.text=@"Sub-Scheme 2";
            break;

        case 2:
            cell.textLabel.text=@"Sub-Scheme 3";
            break;

        case 3:
            cell.textLabel.text=@"Sub-Scheme 4";
            break;
        case 4:
            cell.textLabel.text=@"Sub-Scheme 5";
            break;


        default:
            break;
    }
    }else{
        cell.textLabel.text=@" ";

    }

    return cell;
}

【问题讨论】:

  • 当你说“cell.textlabel.text 在滚动表格视图时重复”是什么意思?你的意思是说,所有单元格的文本在特定部分都是相同的?
  • 请看我在下面发布的答案,这并没有忽略单元格的出列。

标签: ios objective-c iphone xcode uitableview


【解决方案1】:

您必须使用dequeueReusableCellWithIdentifier: 重复使用单元格,以获得更好的性能和内存优化。

您的核心问题是另外一回事。在重新使用之前,您不会重置单元格。我对您的建议是在出列操作后重置单元格文本标签,例如cell.textLabel.text = @"",然后在您的开关中设置您的标签。

我建议您在切换到不重复使用单元格之前尝试一次。

基本上,您的代码应该是这样的:

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

    static NSString *cellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    cell.selectionStyle = UITableViewCellSelectionStyleBlue;

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

    cell.textLabel.textColor = [UIColor blackColor];
    cell.textLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:14.0];
    cell.textLabel.text = @""; // Reset Text Label before re-using

     ... rest of your code...
} 

【讨论】:

  • 通过出列单元格来解决,对吧?而不是通过这里接受的答案,因为这会导致内存问题......
  • 这里我需要一些数组数据来设置单元格仪式中的值?
  • 一点也不...通过“...其余代码...”我的意思是按照您在示例中的方式使用您的 Switch。我只想在出队后重置文本标签。试试这个。
【解决方案2】:

使用不同的单元格标识符

例如像下面这样...

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

    NSString *CellIdentifier = [NSString stringWithFormat:@"%d,%d",indexPath.section,indexPath.row];

    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        /// write your code here..
    }
}

或者像下面这样设置 nil..

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

    //static NSString *CellIdentifier = @"CellIdentifier";    

    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:nil];
    if (cell == nil)
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
        /// write your code here..
    }
}

【讨论】:

  • 设置 dequeueReusableCellWithIdentifier:nil 我认为不是一个好方法。无论如何感谢您的回答
【解决方案3】:

见下面的编码。我试过了。当我滚动表格视图时,cell.textLabel.text 不会重复

    NSMutableArray *arrayRepeat;

在 viewDidLoad 中

   arrayRepeat = [[NSMutableArray alloc]initWithObjects:@"Sub-Scheme 1",@"Sub-Scheme 2",@"Sub-Scheme 3",@"Sub-Scheme 4",@"Sub-Scheme 5", nil];

UITableViewDataSource 方法

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return arrayRepeat.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *CellIdentifier = @"Cell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
   cell.selectionStyle = UITableViewCellSelectionStyleBlue;
   if (cell == nil) 
   {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
   }
   cell.textLabel.textColor = [UIColor blackColor];
   cell.textLabel.font = [UIFont fontWithName:@"Helvetica Neue" size:14.0];
   cell.textLabel.text = [NSString stringWithFormat:@"%@",[arrayRepeat objectAtIndex:indexPath.row]];  

   return cell;
}

【讨论】:

    【解决方案4】:

    不重复使用单元格是实现这一点的最简单方法。

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
        static NSString *CellIdentifier = @"Cell";
    
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    
        cell.textLabel.textColor=[UIColor blackColor];
    
        cell.textLabel.font=[UIFont fontWithName:@"Helvetica Neue" size:14.0];
    
    
        if (indexPath.section == 0) {
    
            switch (indexPath.row) {
                case 0:
                    cell.textLabel.text=@"Sub-Scheme 1";
                    break;
    
                case 1:
                    cell.textLabel.text=@"Sub-Scheme 2";
                    break;
    
                case 2:
                    cell.textLabel.text=@"Sub-Scheme 3";
                    break;
    
                case 3:
                    cell.textLabel.text=@"Sub-Scheme 4";
                    break;
                case 4:
                    cell.textLabel.text=@"Sub-Scheme 5";
                    break;
    
    
                default:
                    break;
    
            }
    
        }else{
            cell.textLabel.text=@" ";
    
        }
    
        return cell;
    }
    

    希望它对你有用。

    【讨论】:

    • 如果你不重复使用单元格,那么它会导致巨大的内存泄漏。
    • 为什么我们不检查单元格 == nil?
    • 因为已经在第二行分配了单元格。分配单元格后,单元格永远不会为空。
    【解决方案5】:

    必须重复使用单元格,并且不将它们出列绝不是一个好的解决方案。您面临的问题当然与单元格的出队有关,但如果为所有情况的文本标签提供值,则可以解决该问题。

    如果你用这个替换你的默认情况可能会解决你的问题:

    default: cell.textLabel.text=@""; break;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-27
      • 2016-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多