UIButton - NSInternalInconsistencyException - 无法将具有标识符 Cell2 的单元格出列
Posted
技术标签:
【中文标题】UIButton - NSInternalInconsistencyException - 无法将具有标识符 Cell2 的单元格出列【英文标题】:UIButton - NSInternalInconsistencyException - unable to dequeue a cell with identifier Cell2 【发布时间】:2016-04-05 18:51:54 【问题描述】:我有一个拆分视图控制器。在拆分视图的表视图选择中。我在右栏按钮项中有 UIButton 。当我单击 UIButton 时。我收到此错误'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier Cell2 - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
现在按下按钮时,会发生以下情况:
- (IBAction)selectCommunityPressed:(id)sender
if(!communitiesSorted)
[self CustomAlert:@"Sorry no data received, if *** and 3G/Wifi are working, please press the back button and try again, else contact software vendor."];
return;
if(communityPopup)
[[communityPopup getPopOver]presentPopoverFromRect:_selectCommunity.bounds inView:_selectCommunity permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
communityPopup 是一个名为 PopupGenerator 的自定义类
这是自定义类.h:
#import <Foundation/Foundation.h>
#import "UIKit/UIKit.h"
@interface PopupGenerator : NSObject
@property (nonatomic,strong) UIViewController *popview;
@property (nonatomic,strong) UIPopoverController *popover;
-(PopupGenerator *)initWithFrame:(CGRect) frame;
-(void)addItemToPopUp:(id)item;
-(UIPopoverController *)getPopOver;
-(UIViewController *)getPopView;
@end
这是自定义类.m:
#import "PopupGenerator.h"
@implementation PopupGenerator
@synthesize popover;
@synthesize popview;
-(PopupGenerator *)initWithFrame:(CGRect) frame
self = [super init];
if(self)
popview=[[UIViewController alloc] init];
[popview.view setBackgroundColor:[UIColor whiteColor]];
[popview.view setFrame:frame];
popover=[[UIPopoverController alloc] initWithContentViewController:popview];
return self;
-(void)addItemToPopUp:(id)item
[popview.view addSubview:item];
-(UIPopoverController *)getPopOver
return popover;
-(UIViewController *)getPopView
return popview;
@end
这个自定义类的主要作用是显示一个弹出窗口,其中包含一个表格视图。
以下是弹出窗口内的 tableview 是如何生成的:
- (void)WaitUntilAreaSort
if([areaData.communityDictionary count]!=0 && areaData.communityDictionary!=nil)
selectedAreaTable = [[UITableView alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 350.0f, 395.0f) style:UITableViewStylePlain];
[selectedAreaTable setBounces:NO];
selectedAreaTable.delegate = self;
selectedAreaTable.dataSource = self;
NSArray *unsortedAreaData = [areaData.communityDictionary valueForKey:[[areaData.communityDictionary allKeys] objectAtIndex:0]];
if (unsortedAreaData == (id)[NSNull null])
[self CustomAlert:@"Error downloading data, please contact software vendor for assistance"];
else
sortedAreaData = [[NSArray alloc]initWithArray: [unsortedAreaData sortedArrayUsingSelector: @selector(caseInsensitiveCompare:)]];
communityPopup = [[PopupGenerator alloc]initWithFrame:CGRectMake(0.0f, 0.0f, 350.0f, 540.0f)];
[communityPopup getPopOver].popoverContentSize = CGSizeMake(350.0f, 540.0f);
[communityPopup addItemToPopUp:selectedAreaTable];
UILabel *sortLabel = [[UILabel alloc]initWithFrame:CGRectMake(115, 395, 120, 30)];
[sortLabel setFont:[UIFont fontWithName:@"Helvetica" size:17]];
[sortLabel setTextAlignment:NSTextAlignmentCenter];
[sortLabel setTextColor:[UIColor whiteColor]];
[sortLabel setBackgroundColor:[UIColor clearColor]];
[sortLabel setText:@"Sort By:"];
[communityPopup addItemToPopUp:sortLabel];
sortLabel = nil;
sortCriteria = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects:@"Alphabetical",@"Closing Date", nil]];
[sortCriteria setFrame:CGRectMake(50, 430, 250, 35)];
[sortCriteria setSelectedSegmentIndex:0];//alphabetical order
[sortCriteria addTarget:self action:@selector(SortCriteriaChanged:) forControlEvents:UIControlEventValueChanged];
[sortCriteria setBackgroundColor:[UIColor clearColor]];
NSUserDefaults *communityPrefs = [NSUserDefaults standardUserDefaults];
NSString *alpha = [communityPrefs objectForKey:@"isAlphabeticalSort"];
if([alpha isEqual: @"YES"])
sortCriteria.selectedSegmentIndex = 0;
else
sortCriteria.selectedSegmentIndex = 1;
[communityPopup addItemToPopUp:sortCriteria];
selectionLabel = [[UILabel alloc]initWithFrame:CGRectMake(50.0f, 465.0f, 250.0f, 30.0f)];
[selectionLabel setTextAlignment:NSTextAlignmentCenter];
[selectionLabel setTextColor:[UIColor blueColor]];
[selectionLabel setBackgroundColor:[UIColor clearColor]];
[selectionLabel setText:@""];
[communityPopup addItemToPopUp:selectionLabel];
selectedAreaOKButton =[UIButton buttonWithType:UIButtonTypeCustom];
selectedAreaOKButton.frame = CGRectMake(125.0f,495.0f,105.0f, 37.0f);
[selectedAreaOKButton setBackgroundColor:[UIColor whiteColor]];
[selectedAreaOKButton setTitle:@"OK" forState:UIControlStateNormal];
[selectedAreaOKButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[selectedAreaOKButton addTarget:self action:@selector(SelectedAreaOKButtonPressed:)forControlEvents:UIControlEventTouchUpInside];
[communityPopup addItemToPopUp:selectedAreaOKButton];
if([[areaData.communityDictionary allKeys]count] != 0)
communitiesSorted = true;
[_selectCommunity setEnabled:YES];
[self StopActivityIndicator];
现在应用程序在按下按钮后崩溃。任何想法为什么我会收到此错误以及如何解决它?我的脑子一片空白。
这是我的 cellForRowAtIndexPath 方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell2" forIndexPath:indexPath];
NSString *object = self.objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
【问题讨论】:
【参考方案1】:你能告诉我们你的 cellForRowAtIndexPath 方法吗?
您可能忘记在情节提要中包含单元标识符或设置单元的类别。
【讨论】:
我发布了我的 cellForRowAIndexPath 我在层板中的单元格确实有一个标识符 “设置单元格的类”我该怎么做@Echizzle以上是关于UIButton - NSInternalInconsistencyException - 无法将具有标识符 Cell2 的单元格出列的主要内容,如果未能解决你的问题,请参考以下文章
从 UIButton 中获取 UIButton 的标签,该 UIButton 位于此 UIButton 呈现的弹出框中
如何删除多个 UIButton 或动态创建的特定 UIButton?
为啥 Swift4 会强制转换 UIButton 数组!到 [UIButton?] 类型?