我正在使用 Storyboard,但我的自定义单元格位于 xib 中。表格在那里,但没有出现单元格。我的代码:
OurTeamCell.h
#import <UIKit/UIKit.h>
@interface OurTeamCell : UITableViewCell
@property (strong, nonatomic) IBOutlet UILabel *LabelNome;
@property (strong, nonatomic) IBOutlet UILabel *LabelOcupacao;
@property (strong, nonatomic) IBOutlet UIImageView *ImageFoto;
@property (strong, nonatomic) IBOutlet UIImageView *fundoCellImage;
@end
OurTeamCell.m
#import "OurTeamCell.h"
#import <QuartzCore/QuartzCore.h>
@implementation OurTeamCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
self.fundoCellImage.layer.cornerRadius = 5;
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
@end
我们的团队.h
#import <UIKit/UIKit.h>
@interface OurTeam : UIViewController
@property (strong, nonatomic) IBOutlet UITableView *tableViewTeam;
@end
我们的团队.m
#import "OurTeam.h"
#import "OurTeamCell.h"
#import <QuartzCore/QuartzCore.h>
#define isiPhone5 ([[UIScreen mainScreen] bounds].size.height == 568)?TRUE:FALSE
@interface OurTeam ()
@end
@implementation OurTeam{
NSMutableDictionary *listaTeam;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tableViewTeam.backgroundColor = [UIColor clearColor];
self.tableViewTeam.opaque = NO;
self.tableViewTeam.backgroundView = nil;
self.tableViewTeam.layer.cornerRadius = 5;
NSMutableArray *listaNomes = [NSMutableArray new];
[listaNomes addObject:@"TESTE"];
[listaTeam setObject:listaNomes forKey:@"nomes"];
if (UIUserInterfaceIdiomPad == UI_USER_INTERFACE_IDIOM()) {
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"fundo~ipad.png"]];
} else {
if (isiPhone5)
{
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"fundo4inch.png"]];
}
else
{
self.view.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"fundo.png"]];
}
}
}
-(int) numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [listaTeam count];
}
- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
CGImageRef imgRef = [image CGImage];
CGImageRef maskRef = [maskImage CGImage];
CGImageRef actualMask = CGImageMaskCreate(CGImageGetWidth(maskRef),
CGImageGetHeight(maskRef),
CGImageGetBitsPerComponent(maskRef),
CGImageGetBitsPerPixel(maskRef),
CGImageGetBytesPerRow(maskRef),
CGImageGetDataProvider(maskRef), NULL, false);
CGImageRef masked = CGImageCreateWithMask(imgRef, actualMask);
return [UIImage imageWithCGImage:masked];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"SimpleTableCell";
OurTeamCell *cell = (OurTeamCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"OurTeamCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.backgroundColor = [UIColor clearColor];
cell.backgroundView = [UIView new];
cell.selectedBackgroundView = [UIView new];
cell.LabelNome.text = [[listaTeam objectForKey:@"nomes"] objectAtIndex:indexPath.row];
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
@end
编辑:
表格 View 是这样显示的:
最佳答案
添加:
[self.tableViewTeam reloadData];
-viewDidLoad
方法中的最后一行代码。
您需要通过调用 -reloadData
方法告诉 TableView 您的数据已更新。
[编辑]
正如 0xFADE 在 OP 注释中所述,您还需要在 numberOfRowsInSection
方法中返回 nomes [[listaTeam objectForKey:@"nomes"] count]
的计数.
关于ios - 表格 View 不适用于自定义单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20863360/