简体   繁体   中英

UITableviewcell cell.textlabel.text repeats when scrolling the tableview? how to avoid the cell.textlabel.text repetation?

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

use cell Identifier different

For example like bellow...

- (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..
    }
}

OR set nil like bellow..

- (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..
    }
}

Do not reuse the cell is the simplest to achieve this.

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

hope it work for you .

You must reuse cell using dequeueReusableCellWithIdentifier: for better performance and memory optimisation.

Your core issue is something else. You are not reseting cell before re-using it. My advise to you would be reset the cell text label after dequeue operation something like this cell.textLabel.text = @"" and then set your labels in your switch.

I recommend trying this once before you switch to not re-using cells.

Essentially, this is how your code should look like:

- (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...
} 

See the below coding.I tried this.When I scroll the tableview the cell.textLabel.text is not repeated

    NSMutableArray *arrayRepeat;

in viewDidLoad

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

UITableViewDataSource Methods

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

Well cells must be reused and not dequeuing them is never a good solution. The problem you are facing is of course related to the dequeuing of the cell but it can be resolved if provide values to the text label for all cases.

If you replace your default case with this one may resolve your issue:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM