Ваш каст ничего не делает, так как вы кастуете в id
.
Хотя ответ @ Шона работает и он делает единственный выход, он довольно уродлив, имея все фигурные скобки, я бы, наверное, выбрал
id headerView = nil; // Initialize to nil... you may not go into either side of your if
if (TAG_A == tableView.tag) {
SectionHeaderViewA *sectionHeaderViewA = [[SectionHeaderViewA alloc] init];
sectionHeaderViewA.textLabelA = ...
headerView = sectionHeaderViewA;
} else if (TAG_B == tableView.tag) {
SectionHeaderViewB *sectionHeaderViewB = [[SectionHeaderViewB alloc] init];
sectionHeaderViewB.textLabelB = ...
headerView = sectionHeaderViewB;
}
return headerView;
Или другая возможность (возможно, из-за проблемы) состоит в том, чтобы заставить sectionHeaderViewA
и sectionHeaderViewB
соответствовать протоколу, и тогда вы можете сделать его еще немного более аккуратным.
SectionHeaderInterface.h
@protocol SectionHeaderInterface <NSObject>
@property (strong, nonatomic) UILabel *textLabel;
@end
SectionHeaderView (A | B) .h
#import "SectionHeaderInterface.h"
@interface SectionHeaderView(A|B) : UIView <SectionHeaderInterface>
// ... rest of interface
@end
SectionHeaderView (A | B) .m
@implementation SectionHeaderView(A|B)
@synthesize textLabel = _textLabel;
// ... rest of your class
@end
YourController.m
id<SectionHeaderInterface> headerView = nil; // Initialize to nil... you may not go into either side of your if
if (TAG_A == tableView.tag) {
headerView = [[SectionHeaderViewA alloc] init];
} else if (TAG_B == tableView.tag) {
headerView = [[SectionHeaderViewB alloc] init];
}
headerView.textLabel.text = ...
return headerView;