Search code examples
iosiphoneobjective-cios6uitableview

ViewwithTag does not fetch cell label


I am trying to display the cell label text using the tag name. In my mainstory board in the default cell, I have added a lable and added the tag name as 70. I am trying load that data using the below method . Nothing displays out. Please help me.

[Crop Cropname] is not empty, cell.textLabel.text = [crop cropName] works fine.I am just trying to format things.




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

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
        // Configure the cell...

         Crop * crop = [filteredItemist objectAtIndex:[indexPath row]];

        cell.detailTextLabel.text = [NSString stringWithFormat:@"%d",crop.cropId];

        UILabel *cropNameLabel = (UILabel *)[cell viewWithTag:70];
        cropNameLabel.text = [crop cropName];


        return cell;

    }

Solution

  • You should check that the Cell Identifier of the cell you are using in the storyboard is using the "Cell" reuse identifier you are using to dequeue a cell.

    What could be happening is that you are not actually getting a cell from the xib, because it doesn't have a Cell with that identifier, but then you handle a nil cell by creating a new cell:

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
            cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        }
    

    Unfortunately, this new cell doesn't have the added label with a tag of 70 that you added in the storyboard...