ios - 按下指向 TableView 的按钮时应用程序崩溃

标签 ios objective-c uitableview uibutton nsuserdefaults

我有一个指向 View 的按钮,每当我看到按下该按钮时,我的应用程序就会崩溃。谁能告诉我为什么?该代码是按钮推送到的 View 的实现文件。

@implementation OpenShiftViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    myDictionary = [defaults dictionaryRepresentation];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 1; 
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return myDictionary.count; 
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    static NSString *CellIdentifier = @"cell";
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    NSArray * allKeys = [myDictionary allKeys];
    cell.textLabel.text = [myDictionary objectForKey:[allKeys objectAtIndex:indexPath.row]];
    cell.detailTextLabel.text = [myDictionary objectForKey:[allKeys     objectAtIndex:indexPath.row]];
    return cell; 
}

@end

最佳答案

- (void)viewDidLoad

{

[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
myDictionary = [defaults dictionaryRepresentation];


myMutableArray = [[NSMutableArray alloc]init];
[self storingObjectsFromDictionaryToArray];
// Do any additional setup after loading the view, typically from a nib.

-(void)storingObjectsFromDictionaryToArray

{

NSArray *arr = [myDictionary allKeys];


  [myMutableArray addObjectsFromArray:[myDictionary valueForKey:[arr objectAtIndex:0]]];
NSString *str = [NSString stringWithFormat:@"%@",[myDictionary valueForKey:[arr objectAtIndex:1]]];
[myMutableArray addObject:str];
str = [NSString stringWithFormat:@"%@",[myDictionary valueForKey:[arr objectAtIndex:2]]];
[myMutableArray addObject:str];
 [myMutableArray addObjectsFromArray:[myDictionary valueForKey:[arr objectAtIndex:3]]];
 [myMutableArray addObject:[myDictionary valueForKey:[arr objectAtIndex:4]]];
 [myMutableArray addObjectsFromArray:[myDictionary valueForKey:[arr objectAtIndex:5]]];
 [myMutableArray addObjectsFromArray:[myDictionary valueForKey:[arr objectAtIndex:6]]];
 [myMutableArray addObject:[myDictionary valueForKey:[arr objectAtIndex:7]]];

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return myMutableArray.count;

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CellIdentifier = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

}



 cell.textLabel.text = [myMutableArray objectAtIndex:indexPath.row];
cell.detailTextLabel.text = [myMutableArray objectAtIndex:indexPath.row];


return cell;

关于ios - 按下指向 TableView 的按钮时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20330857/

相关文章:

objective-c - 在 Objective-C 中将 @ 放在整数前意味着什么?

objective-c - 矩阵信息提取

iphone - NSFetchedResultsController 添加实体在 tableView 中插入空白行

iphone - 七层信息层次 - UITableView - iPhone

ios - 事件工具包日历未显示在 iCal 中

javascript - react native 自定义组件

ios - 在 TextField 中放置 PlaceHolder

ios - 我有多个 Storyboard。如何使用 AppDelegate 在另一个 Storyboard中打开另一个 ViewController? (继续)

iphone - iOS MVC - 来回传递数据以查看 Controller 和模型

ios - Xcode 8.2.1/Swift 3 - 从 Plist 字典数组加载 TableView