我正在使用我在检查 numberOfRowsInSection: 方法时添加到标题视图中的 UIButton,并且在按钮状态下,我正在检查该部分中是否存在行。但是我的应用程序崩溃时[self.tableView headerViewForSection:section] 返回 nil。由于它对于可见的部分工作正常,因此我假设这种情况正在发生,因为我的部分目前不可见。
以下是我创建部分标题视图的方法:
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
UITableViewHeaderFooterView *myHeader = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HeaderIdentifier];;
if(!myHeader) {
myHeader = [[UITableViewHeaderFooterView alloc] initWithReuseIdentifier:HeaderIdentifier];
}
UIView *sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
sectionHeaderView.tag = 1001;
sectionHeaderView.backgroundColor = [UIColor whiteColor];
NSDictionary *sectionDict = [self.sectionArray objectAtIndex:section];
NSString *sectionName = [sectionDict objectForKey:@"name"];
if ([sectionName isEqualToString:@"Verified Seller"]) {
UIImageView *sectionHeaderImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 44, 44)];
sectionHeaderImageView.image = [UIImage imageNamed:@"blue_ok.png"];
[sectionHeaderImageView setContentMode:UIViewContentModeCenter];
[sectionHeaderView addSubview:sectionHeaderImageView];
UILabel *sectionHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(74, 5, 200, 34)];
sectionHeaderLabel.textColor = [UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1];
sectionHeaderLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:16];
sectionHeaderLabel.text = @"Verified Seller";
[sectionHeaderView addSubview:sectionHeaderLabel];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(15, 43, 305, 1)];
view.backgroundColor = [UIColor colorWithRed:217.0f/255.0f green:217.0f/255.0f blue:217.0f/255.0f alpha:1];
[sectionHeaderView addSubview:view];
}
else if ([sectionName isEqualToString:@"Pro-Seller"]){
UIImageView *sectionHeaderImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 44, 44)];
sectionHeaderImageView.image = [UIImage imageNamed:@"blue_ok.png"];
[sectionHeaderImageView setContentMode:UIViewContentModeCenter];
[sectionHeaderView addSubview:sectionHeaderImageView];
UILabel *sectionHeaderLabel = [[UILabel alloc] initWithFrame:CGRectMake(74, 5, 200, 34)];
sectionHeaderLabel.textColor = [UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1];
sectionHeaderLabel.font = [UIFont fontWithName:@"Avenir-Medium" size:16];
sectionHeaderLabel.text = @"Pro Seller";
[sectionHeaderView addSubview:sectionHeaderLabel];
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(15, 43, 305, 1)];
view.backgroundColor = [UIColor colorWithRed:217.0f/255.0f green:217.0f/255.0f blue:217.0f/255.0f alpha:1];
[sectionHeaderView addSubview:view];
}
else if ([sectionName isEqualToString:@"Diagnostic"]){
UIButton *diagnosticBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[diagnosticBtn setTitle:@"Diagnostic Report" forState:UIControlStateNormal];
[diagnosticBtn setTitle:@"Diagnostic Report" forState:UIControlStateSelected];
[diagnosticBtn setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
[diagnosticBtn setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateSelected];
[diagnosticBtn setFont:[UIFont fontWithName:@"Avenir-Medium" size:16]];
[diagnosticBtn setImage:[UIImage imageNamed:@"expand_blue.png"] forState:UIControlStateNormal];
[diagnosticBtn setImage:[UIImage imageNamed:@"collapse_blue.png"] forState:UIControlStateSelected];
[diagnosticBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 290, 0, 0)];
[diagnosticBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -170, 0, 0)];
diagnosticBtn.tag = section;
[diagnosticBtn addTarget:self action:@selector(sectionHeaderBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
[sectionHeaderView addSubview:diagnosticBtn];
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(15, 0, 305, 1)];
view1.backgroundColor = [UIColor colorWithRed:217.0f/255.0f green:217.0f/255.0f blue:217.0f/255.0f alpha:1];
[sectionHeaderView addSubview:view1];
}
else if ([sectionName isEqualToString:@"Certification"]){
UIButton *certificationBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[certificationBtn setTitle:@"Certification" forState:UIControlStateNormal];
[certificationBtn setTitle:@"Certification" forState:UIControlStateSelected];
[certificationBtn setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
[certificationBtn setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateSelected];
[certificationBtn setFont:[UIFont fontWithName:@"Avenir-Medium" size:16]];
[certificationBtn setImage:[UIImage imageNamed:@"expand_blue.png"] forState:UIControlStateNormal];
[certificationBtn setImage:[UIImage imageNamed:@"collapse_blue.png"] forState:UIControlStateSelected];
[certificationBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 290, 0, 0)];
[certificationBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -210, 0, 0)];
certificationBtn.tag = section;
[certificationBtn addTarget:self action:@selector(sectionHeaderBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
[sectionHeaderView addSubview:certificationBtn];
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(15, 0, 305, 1)];
view1.backgroundColor = [UIColor colorWithRed:217.0f/255.0f green:217.0f/255.0f blue:217.0f/255.0f alpha:1];
[sectionHeaderView addSubview:view1];
}
else if ([sectionName isEqualToString:@"Seller Declaration"]){
UIButton *sellerDeclarationBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[sellerDeclarationBtn setTitle:@"Seller Declaration" forState:UIControlStateNormal];
[sellerDeclarationBtn setTitle:@"Seller Declaration" forState:UIControlStateSelected];
[sellerDeclarationBtn setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
[sellerDeclarationBtn setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateSelected];
[sellerDeclarationBtn setFont:[UIFont fontWithName:@"Avenir-Medium" size:16]];
[sellerDeclarationBtn setImage:[UIImage imageNamed:@"expand_blue.png"] forState:UIControlStateNormal];
[sellerDeclarationBtn setImage:[UIImage imageNamed:@"collapse_blue.png"] forState:UIControlStateSelected];
[sellerDeclarationBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 290, 0, 0)];
[sellerDeclarationBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -170, 0, 0)];
sellerDeclarationBtn.tag = section;
[sellerDeclarationBtn addTarget:self action:@selector(sectionHeaderBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
[sectionHeaderView addSubview:sellerDeclarationBtn];
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(15, 0, 305, 1)];
view1.backgroundColor = [UIColor colorWithRed:217.0f/255.0f green:217.0f/255.0f blue:217.0f/255.0f alpha:1];
[sectionHeaderView addSubview:view1];
}
else if ([sectionName isEqualToString:@"Others"]){
UIButton *otherTrustFactorsBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[otherTrustFactorsBtn setTitle:@"Other Trust Factor" forState:UIControlStateNormal];
[otherTrustFactorsBtn setTitle:@"Other Trust Factor" forState:UIControlStateSelected];
[otherTrustFactorsBtn setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
[otherTrustFactorsBtn setTitleColor:[UIColor colorWithRed:51.0f/255.0f green:51.0f/255.0f blue:51.0f/255.0f alpha:1.0f] forState:UIControlStateSelected];
[otherTrustFactorsBtn setFont:[UIFont fontWithName:@"Avenir-Medium" size:16]];
[otherTrustFactorsBtn setImage:[UIImage imageNamed:@"expand_blue.png"] forState:UIControlStateNormal];
[otherTrustFactorsBtn setImage:[UIImage imageNamed:@"collapse_blue.png"] forState:UIControlStateSelected];
[otherTrustFactorsBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 290, 0, 0)];
[otherTrustFactorsBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, -165, 0, 0)];
otherTrustFactorsBtn.tag = section;
[otherTrustFactorsBtn addTarget:self action:@selector(sectionHeaderBtnTapped:) forControlEvents:UIControlEventTouchUpInside];
[sectionHeaderView addSubview:otherTrustFactorsBtn];
UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(15, 0, 305, 1)];
view1.backgroundColor = [UIColor colorWithRed:217.0f/255.0f green:217.0f/255.0f blue:217.0f/255.0f alpha:1];
[sectionHeaderView addSubview:view1];
}
[myHeader addSubview:sectionHeaderView];
return myHeader;
}
以下是我如何在 UITableView 中填充数据的方式 数量行In部分:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSDictionary *sectionSuperDict = [self.sectionArray objectAtIndex:section];
NSString *sectionName = [sectionSuperDict objectForKey:@"name"];
NSDictionary *sectionDict = [sectionSuperDict objectForKey:@"value"];
UIView *sectionHeaderView = [self.tableView headerViewForSection:section];
UIButton *button;
for (UIView *subView in sectionHeaderView.subviews) {
if (subView.tag == 1001) {
for (UIView *subSubView in subView.subviews) {
if ([subSubView isKindOfClass:[UIButton class]]) {
button = (UIButton *)subSubView;
}
}
}
}
NSInteger optionsCount = 0;
if ([sectionName isEqualToString:@"Verified Seller"]) {
}
else if ([sectionName isEqualToString:@"Pro-Seller"]){
}
else if ([sectionName isEqualToString:@"Diagnostic"]){
if (!button.selected) {
optionsCount = 0;
}
else{
if ([sectionDict isKindOfClass:[NSArray class]]) {
optionsCount = 1;
}
else{
NSString *documentSource = [sectionDict objectForKey:@"document_source"];
if ([documentSource isEqualToString:@"none"]) {
optionsCount = 1;
}
else{
optionsCount = 3;
if ([sectionDict objectForKey:@"documents"]) {
optionsCount +=1;
}
if ([sectionDict objectForKey:@"document_note"]) {
if (![[sectionDict objectForKey:@"document_note"] isEqualToString:@""]) {
optionsCount +=1;
}
}
}
}
}
}
else if ([sectionName isEqualToString:@"Certification"]){
if (!button.selected) {
optionsCount = 0;
}
else{
if ([sectionDict isKindOfClass:[NSArray class]]) {
optionsCount = 1;
}
else{
NSString *documentSource = [sectionDict objectForKey:@"document_source"];
if ([documentSource isEqualToString:@"none"]) {
optionsCount = 1;
}
else{
optionsCount = 3;
if ([sectionDict objectForKey:@"documents"]) {
optionsCount +=1;
}
if ([sectionDict objectForKey:@"document_note"]) {
if (![[sectionDict objectForKey:@"document_note"] isEqualToString:@""]) {
optionsCount +=1;
}
}
}
}
}
}
else if ([sectionName isEqualToString:@"Seller Declaration"]){
if (!button.selected) {
optionsCount = 0;
}
else{
if ([sectionDict isKindOfClass:[NSDictionary class]]) {
NSDictionary *valueDict = [sectionDict objectForKey:@"value"];
if ([valueDict isKindOfClass:[NSDictionary class]]) {
if ([[valueDict allKeys] count]) {
NSDictionary *acceptDict = [valueDict objectForKey:@"accept"];
if ([acceptDict isKindOfClass:[NSDictionary class]]) {
if ([[acceptDict allKeys] count]) {
NSString *acceptLabelString = [acceptDict objectForKey:@"label"];
if (![acceptLabelString isEqualToString:@""]) {
optionsCount +=1;
}
}
}
NSDictionary *declarationsDict = [valueDict objectForKey:@"declarations"];
if ([declarationsDict isKindOfClass:[NSDictionary class]]) {
optionsCount = optionsCount + [[declarationsDict allKeys] count];
}
}
else{
optionsCount = 1;
}
}
else{
optionsCount = 1;
}
}
else{
optionsCount = 1;
}
}
}
else if ([sectionName isEqualToString:@"Others"]){
if (!button.selected) {
optionsCount = 0;
}
else{
optionsCount = 1;
NSDictionary *serviceLogDict = [sectionDict objectForKey:@"service_log"];
if ([serviceLogDict isKindOfClass:[NSDictionary class]]) {
if ([[serviceLogDict allKeys] count]) {
//optionsCount +=1;
}
}
NSDictionary *registrationDict = [sectionDict objectForKey:@"registration"];
NSDictionary *insuranceDict = [sectionDict objectForKey:@"insurance"];
if ([registrationDict isKindOfClass:[NSDictionary class]] && [insuranceDict isKindOfClass:[NSDictionary class]]) {
if ([[registrationDict allKeys] count] || [[insuranceDict allKeys] count]) {
optionsCount +=1;
}
}
}
}
return optionsCount;
}
由于按钮在此处返回 nil,而在更新第 5 节的 rowCount 为 1 之前,它会引发异常。应该怎么做才能
[self.tableView headerViewForSection:section]
不为不可见部分返回零吗?
这不是您问题的答案,但可能是您目的的解决方法:
我会向sectionArray
中的项目添加一个键值对(例如:"visible" = "true/false/0/1/..."
)。在部分的按钮操作上修改此值。然后,读取numberOfRowsInSection...
方法中每个项的值,以返回0
或sectionArray.count
,具体取决于该部分当前是否可见。
希望对您有所帮助。
用于在各个部分的任何位置访问表视图标题的 Swift 解决方案
let tblHeaderView = tableView(YourTableView, viewForHeaderInSection: 0)