简体   繁体   中英

UILabel created at didSelectRowAtIndexPath keep repeating at same position

Hi I have a problem with UILabel repeating the same position at UITableView .

I have a custom cell and three UILabels . I only display first Label at cellForRowAtIndexPath and the other two at didSelectRowAtIndexPath .

When I tap the cell I want display two more Labels display but they are repeating at the same position when I scroll the TableView.

Please help me

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"English";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (isSearchOn) 
    {
         static NSString *CellIdentifier = @"English";
         UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
         NSString *cellValue = _searchResult [indexPath.row];
         cell.textLabel.text = cellValue;
         [cell.textLabel setFont:[UIFont fontWithName:@"Helvetica LT Std" size:22]];
    }
    else
    {
        ConversationInEnglish *con = _conversationsInfosnew [indexPath.row];
        _englishLabel = (UILabel *) [cell viewWithTag:10];
        _englishLabel.text = con.english;
        [_englishLabel setFont:[UIFont fontWithName:@"Helvetica LT Std" size:22]];
        UIButton *starButton = [UIButton buttonWithType:UIButtonTypeCustom];
        starButton.frame = CGRectMake(0 , 0 , 60, 37);
        UIImage *btnImage = [UIImage imageNamed:@"star.png"];
        [starButton setImage:btnImage forState:UIControlStateNormal];
        starButton.tag = indexPath.row;
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.accessoryView = starButton;
    }
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.expandedCellIndexPath isEqual:indexPath]){
    }
    else 
    {
        ConversationInEnglish *con = _conversationsInfosnew [indexPath.row];
        UITableViewCell *cell1 = [self.tableView cellForRowAtIndexPath:indexPath];
        _myanmarLabel = (UILabel *)[cell1 viewWithTag:222];
        _myanmarLabel.tag = indexPath.row;
        _myanmarLabel.text = con.myanmar;
        [_myanmarLabel setFont: [UIFont fontWithName:@"Masterpiece Uni Sans" size:16]];
        [cell1.contentView addSubview:_myanmarLabel];
        _tonebasedLabel = (UILabel *)[cell1 viewWithTag:333];
        _tonebasedLabel.tag = indexPath.row;
        _tonebasedLabel.text = con.tone_based;
        [_tonebasedLabel setFont:[UIFont fontWithName:@"Helvetica LT Std" size:15]];
        [cell1.contentView addSubview:_tonebasedLabel];
        _speaker = [UIButton buttonWithType:UIButtonTypeCustom];
        _speaker.frame = CGRectMake(100 , 80 , 150, 37);
        UIImage *btnImage = [UIImage imageNamed:@"speaker.png"];
        [_speaker setImage:btnImage forState:UIControlStateNormal];
        _speaker.tag = indexPath.row;
        [cell1.contentView addSubview:_speaker];
        self.expandedCellIndexPath = indexPath;
        self.expandedCellHeight = 110.0f;
    }
    [self.tableView beginUpdates];
    [self.tableView endUpdates];
}

The code where the UILabels are created do not appear to be in the snippet you provided, so I cannot be sure this is your problem, but check that the origin points of your UILabel frames are not too close together. One of the origin points may need to have its x value increased to separate the two views.

尝试将yourtextfield.hidden = yes放在indexpath的行中,以便每次滚动时都将隐藏该文本字段,并且在didselect中可以显示.hope

The simplest way to get it done is as follow:

First of all have one mutable array with same size with your data array (let it be like this @property (nonatomic,strong) NSMutableArray *searchActive; ).

Then in tableView: didSelectRowAtIndexPath: method add the below line: [self.searchActive setObject:@"1" atIndexedSubscript:indexPath.row]; (here I use text data in array you can use any thing even you can create your data array as mutable array with dictionary objects, where each dictionary contains two data one is for display in table and second is to deiced which cell is tapped).

Below this line and in between the [self.tableView beginUpdates];<#HERE#> [self.tableView endUpdates];

just add one line [self.tableView reloadData];

And in the tableView: cellForRowAtIndexPath: add the conditional statement as below:

if([[self.searchActive objectAtIndex:indexPath.row] isEqualToString:@"1"]) { <# display additional controls here or do what you want to change for the cell#> }

May this will solve your problem :)

EDIT After declaring property you need to @synthesize it in .m file.

And After That just put one statement like self.activeCell = [NSMutableArray alloc]initWithArray:<#YOUR TableView's Data Source Array#>]; ( Here I assume that your tableview data source contains string data )

This is also necessary to have your activeCell array at least that much of count as your tableview's data source array have.

Put the above statement inside viewDidLoad method.

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