Custom UITableViewCell does not show the labels in storyboard

纵然是瞬间 提交于 2020-01-02 07:18:46

问题


In this screen shot you can see that I have added UITableView in UIViewController then customized the UITableViewCell by adding some labels in it. But the issue is when I run the application. All of the cells are empty. There are no labels at all.

I am not getting what can be the issue. I have searched the web and read tutorials but couldn't resolve it.


回答1:


I resolved this issue by myself, just after little effort.
Actually when you create a custom cell you have to do these things:

  1. Setup the labels, images etc on storyboard cell.
  2. Create a custom cell class (inheriting from UITableViewCell)(CustomCell.h & .m), CustomCell.h having all of the properties as iboutlet for labels, images etc. and synthesize them all in implementation.
  3. After creating this custom class go back to storyboard, select the custom cell, change its class to the CustomCell and give it some identifier like "MyCustomCell", then right click on the custom cell and connect the IBOutlets with labels etc.
  4. Now import CustomCell class in the class where you are implementing the UITableView and use the properties you defined in CustomCell class.

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *MyIdentifier = @"MyCustomCell";
    
        CustomCell*cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    
        if (cell == nil)
        {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                           reuseIdentifier:MyIdentifier] autorelease];
        }
    
        // Here we use the provided setImageWithURL: method to load the web image
        // Ensure you use a placeholder image otherwise cells will be initialized with no image
        [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"]
                       placeholderImage:[UIImage imageNamed:@"placeholder"]];
            cell.myCustomLabel.text = @"My Text";
        return cell;
    }
    

I did this all and my issue was resolved and please don't forget to connect the delegates & datasource and table.

Hope this will help others.




回答2:


It is little bit late but you can solve your problem by setting the background color of your view as Clear Color from your storyboard.




回答3:


in the tableview's delegate method , cellforrowatindexpath has a uitableviewcell inside it , there should be an identifier in the initialization of this cell. possibly it is "cell" or "cellIdentifier" .

you just need to select your cell from storyboard and enter this identifier string to the storyboard , where uitableviewcell's attribute inspector stays.

hope this helps.. :)




回答4:


First Set cell identifier in storyboard @"Cell"

then set tag of label

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

// Create
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}



UILabel *lblDate = (UILabel*)[cell viewWithTag:101];
UILabel *lblDistance = (UILabel*)[cell viewWithTag:102];
UILabel *lbltype = (UILabel*)[cell viewWithTag:103];

lblDate.text = [temp objectAtIndex:indexPath.row] valueForKey:@"date"] stringByReplacingOccurrencesOfString:@"-" withString:@"/"]];
lblDistance.text = [NSString stringWithFormat:@"%@ KM",[[temp objectAtIndex:indexPath.row] valueForKey:@"distance"]];

NSString *type = [NSString stringWithFormat:@"%@",[[temp objectAtIndex:indexPath.row] valueForKey:@"distance"]];
if([type isEqualToString:@"0"])
{
    lbltype.text = @"Personal";
}
else
{
    lbltype.text = @"Bussiness";
}
// Configure
return cell;
}


来源:https://stackoverflow.com/questions/11339054/custom-uitableviewcell-does-not-show-the-labels-in-storyboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!