0

I have tableview. When I scrolling my tableview my text label in table cells is mixing. How to fix it?

My code:

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

static NSString *cellIdentifier = @"Cell3";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

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

}

    UILabel *label = (UILabel *)[self.view viewWithTag:1];

    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
                label.text = [_array objectAtIndex:indexPath.row];
                break;
            case 1:
                label.text = [_array objectAtIndex:indexPath.row];
                break;
            case 2:
                label.text = [_array objectAtIndex:indexPath.row];
                break;


        }}

1 Answers1

2

you do not set a text to label on the cell, because self.view has many views with tag 1. (any cell at the view has such view).
To set text into cell you should use one of subviews of the cell (textLabel, detailTextLabel or some custom label), for example:

<...>
cell.textLabel.text = "Hello";
<...>

I think, we need add some explanation for you.
Method -(UIView*)viewWithTag: finds in subviews tree (In all views that lie on the parent view). The tag is just a number, nothing else, more then one view in the subviews tree can have same tag. When the tableView can't find a cell to reuse (dequeueReusableCellWithIdentifier: returns nil) you create new cell that does not lie on the table (parent view for the cell is nil), therefore at first time you can't find a label with tag 1. At second time and others you get one of labels on the tableView (not subview of the cell, but subview of other cell).

About good stile and tag
Sometimes tag is very useful mechanism to make our life easy, for example, if you have dynamic array of buttons it is very useful to set to them same selector and target and tag same to a index in array and determine which button was selected by the tag property. But tags are not so useful for finding subviews, because you should know some strange number, and should cast type of a returned UIView* to needed type. I do not think that it is good. A second reason, that you have 2 different places where you should set same value. A third reason lies in a field of data collision, you can set same tag for different views, after that you will get wrong result, and finding error will be very difficult.

Andrew Romanov
  • 4,774
  • 3
  • 25
  • 40
  • I need to create label in storyboard. In this case I can't use `cell.textLabel.text = "Hello";` –  Oct 21 '17 at 14:04
  • You can use a custom subclass of UITableViewCell and add to it a property with IBOutlet modifier that will contain reference to your label. – Andrew Romanov Oct 21 '17 at 14:05
  • Ok. Are there any other ways? –  Oct 21 '17 at 14:06
  • Why do you need other way? – Andrew Romanov Oct 21 '17 at 14:07
  • also you can replace (UILabel *)[self.view viewWithTag:1]; with (UILabel *)[cell.contentView viewWithTag:1]; this will fix problem. But it is a bad style code – Andrew Romanov Oct 21 '17 at 14:08
  • It is works! Why `(UILabel *)[cell.contentView viewWithTag:1]; ` is bad style code ? –  Oct 21 '17 at 14:57
  • I've updated my answer, if someone can understand it, please edit it to correct English (it is not my native language) – Andrew Romanov Oct 21 '17 at 15:15
  • Could you check this question please https://stackoverflow.com/questions/46843893/search-bar-in-tableview . May be I have same problem? –  Oct 23 '17 at 04:50
  • @AndrewRomanov I understand this is not welcomed by the community. But could you please help with this problem. I just don't know what to do. Thanks! - https://stackoverflow.com/questions/69851479/audio-files-wont-play-with-airpods-pro-on-ios-15 – user Nov 12 '21 at 20:34