- (void)viewDidLoad
{
[super viewDidLoad];
UINib *tNIb = [UINib nibWithNibName:@"textFieldNib" bundle:nil];
[self.tableView registerNib:tNIb forCellReuseIdentifier:@"textCell"];
UINib *dNib = [UINib nibWithNibName:@"descriptionNib" bundle:nil];
[self.tableView registerNib:dNib forCellReuseIdentifier:@"descriptionCell"];
UINib *iNib = [UINib nibWithNibName:@"imageNib" bundle:nil];
[self.tableView registerNib:iNib forCellReuseIdentifier:@"imageCell"];
UINib *lNib = [UINib nibWithNibName:@"labelNib" bundle:nil];
[self.tableView registerNib:lNib forCellReuseIdentifier:@"labelCell"];
NSLog(@"%@ %@ %@", self.collegeString, self.presidentString, self.membersString);
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myCell" forIndexPath:indexPath];
_textCell = (textFieldCell *) [self.tableView dequeueReusableCellWithIdentifier:@"textCell"];
_descriptionCell = (descriptionCell *) [self.tableView dequeueReusableCellWithIdentifier:@"descriptionCell"];
_imageCell = (imageCell *) [self.tableView dequeueReusableCellWithIdentifier:@"imageCell"];
_labelCell = (labelCell *) [self.tableView dequeueReusableCellWithIdentifier:@"labelCell"];
if (indexPath.section == 1) {
_labelCell.collegeLabel.text = self.collegeString;
_labelCell.presidentLabel.text = self.presidentString;
_labelCell.membersLabel.text = self.membersString;
return _labelCell;
}
if (indexPath.section == 2) {
return _imageCell;
}
if (indexPath.section == 3) {
return _descriptionCell;
}
if (indexPath.section == 4) {
return _textCell;
}
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"Height for row called");
if (indexPath.section == 1) {
return 100.0f;
}
if (indexPath.section == 2) {
return 100.0f;
}
if (indexPath.section == 3) {
return 130.0f;
}
if (indexPath.section == 4) {
return 110.0f;
}
else{
return 90.0f;
}
}
我使用nibs文件制作了四个不同的自定义单元格,每个单元格与自己的UITableViewCell .h / .m文件配对。它们各有不同的尺寸。我不知道为什么没有在tableView中显示。
答案 0 :(得分:0)
试试这个......
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TextFieldCell *textCell = [tableView dequeueReusableCellWithIdentifier:@"textCell"];
ImageCell *imageCell = [tableView dequeueReusableCellWithIdentifier:@"imageCell"];
LabelCell *labelCell = [tableView dequeueReusableCellWithIdentifier:@"labelCell"];
if (textCell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"textFieldNib" owner:self options:nil];
textCell = [topLevelObjects objectAtIndex:0];
}
if (imageCell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"imageCellNib" owner:self options:nil];
imageCell = [topLevelObjects objectAtIndex:0];
}
if (labelCell == nil)
{
NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"labelCellNib" owner:self options:nil];
labelCell = [topLevelObjects objectAtIndex:0];
}
if (indexPath.section == 1)
{
labelCell.collegeLabel.text = self.collegeString;
labelCell.presidentLabel.text = self.presidentString;
labelCell.membersLabel.text = self.membersString;
return labelCell;
}
else if (indexPath.section == 2)
{
return imageCell;
}
else if (indexPath.section == 3)
{
return textCell;
}
}
答案 1 :(得分:0)
这应该是您的-cellForRowAtIndexPath:
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
case 0: {
textFieldNib *textCell = [tableView dequeueReusableCellWithIdentifier:@"textCell"
forIndexPath:indexPath];
//set up textCell
textCell.collegeLabel.text = self.collegeString;
textCell.presidentLabel.text = self.presidentString;
textCell.membersLabel.text = self.membersString;
return textCell;
}
case 1: {
descriptionNib *descriptionCell = [tableView dequeueReusableCellWithIdentifier:@"descriptionCell"
forIndexPath:indexPath];
//set up descriptionCell
return descriptionCell;
}
case 2: {
imageNib *imageCell = [tableView dequeueReusableCellWithIdentifier:@"imageCell"
forIndexPath:indexPath];
//set up imageCell
return imageCell;
}
case 3: {
labelNib *labelCell = [tableView dequeueReusableCellWithIdentifier:@"labelCell"
forIndexPath:indexPath];
//set up labelCell
return labelCell;
}
default: {
//none of the cases worked so return default UITableViewCell to prevent crash
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"
forIndexPath:indexPath];
return cell;
}
}
}
当您不执行-registerNib:forCellReuseIdentifier:
时,请在-cellForRowAtIndexPath:
中加载nib作为:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
switch (indexPath.section) {
case 0: {
textFieldNib *textCell = [tableView dequeueReusableCellWithIdentifier:@"textCell"];
if (textCell == nil) {
textCell = [[NSBundle mainBundle] loadNibNamed:@"textFieldNib" owner:self options:nil][0];
}
//set up textCell
textCell.collegeLabel.text = self.collegeString;
textCell.presidentLabel.text = self.presidentString;
textCell.membersLabel.text = self.membersString;
return textCell;
}
case 1: {
descriptionNib *descriptionCell = [tableView dequeueReusableCellWithIdentifier:@"descriptionCell"];
if (descriptionCell == nil) {
descriptionCell = [[NSBundle mainBundle] loadNibNamed:@"descriptionNib" owner:self options:nil][0];
}
//set up descriptionCell
return descriptionCell;
}
case 2: {
imageNib *imageCell = [tableView dequeueReusableCellWithIdentifier:@"imageCell"];
if (imageCell == nil) {
imageCell = [[NSBundle mainBundle] loadNibNamed:@"imageNib" owner:self options:nil][0];
}
//set up imageCell
return imageCell;
}
case 3: {
labelNib *labelCell = [tableView dequeueReusableCellWithIdentifier:@"labelCell"];
if (labelCell == nil) {
labelCell = [[NSBundle mainBundle] loadNibNamed:@"labelNib" owner:self options:nil][0];
}
//set up labelCell
return labelCell;
}
default: {
//none of the cases worked so return default UITableViewCell to prevent crash
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
return cell;
}
}
}
PS:不要制作全球UITableViewCell
对象,例如_textCell
和_descriptionCell
等。这只是浪费而且坦率地说......对大多数用途都没用。