Ios 使用NSUserdefaults填充UITableView

Ios 使用NSUserdefaults填充UITableView,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我一直在尝试让tableview正常工作。每天我都在取得一些进步,但通过测试应用程序,我看不到任何反馈,所以我不确定我是否做了正确的事情 我在谷歌上搜索了一段时间,查看了苹果的文档,但我仍然不明白如何正确地填充UITableView 第一个问题。我在哪能拿到手机?我需要制作一个自定义单元格来显示数据。解决方案似乎使用nib,但如何将其加载到另一个视图控制器上 第二个问题。这个单元格显然应该有动态内容。该内容将从用户默认设置中获取(我可以这样做),但我不知道如何访问NIB中的标签来实现这一点 第三

我一直在尝试让tableview正常工作。每天我都在取得一些进步,但通过测试应用程序,我看不到任何反馈,所以我不确定我是否做了正确的事情

我在谷歌上搜索了一段时间,查看了苹果的文档,但我仍然不明白如何正确地填充UITableView

第一个问题。我在哪能拿到手机?我需要制作一个自定义单元格来显示数据。解决方案似乎使用nib,但如何将其加载到另一个视图控制器上

第二个问题。这个单元格显然应该有动态内容。该内容将从用户默认设置中获取(我可以这样做),但我不知道如何访问NIB中的标签来实现这一点

第三个问题。我希望有不同的部分(每个部分有不同类型的单元格显示),有不同的页眉/页脚。和往常一样,我发现这里缺少文档。我怎样才能做到这一点

第四,也是最重要的问题。我不知道如何装电池。显然,这是由
cellforrowatinexpath
完成的,但我不确定它是如何做到这一点的

最后,这里是代码(以及stacktrace)。希望你能帮助我请不要关闭此复制。首先,它不应该是重复的,其次,如果重复的问题是在3年多前因为图书馆更新了多少而提出的,那么它就不是重复的。

#import "profileViewController.h"
#import "../Objs/CCProfileObject.h"

@interface profileViewController ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UITableViewCell *proxySampleCell;
@property (strong, nonatomic) IBOutlet UITableViewCell *ccSampleCell;
@end

@implementation profileViewController
@synthesize tableView;

- (void)viewDidLoad{
    // Dark mode, you can skip all of this
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(changeColor:)
                                                 name:@"darkToggle" object:nil];

    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if([userDefaults boolForKey:@"darkMode"] == YES){
        [self changeColor:nil];
    }

}

- (void)viewDidAppear:(BOOL)animated{

    // Display profiles
    NSData *arrayData = [[NSUserDefaults standardUserDefaults] objectForKey:@"profileArray"];
    NSArray *profiles = [NSKeyedUnarchiver unarchiveObjectWithData:arrayData];

    for(NSObject *profile in profiles){
        if([profile class] == [CCProfileObject class])
        {
            CCProfileObject *ccProfile = (CCProfileObject*) profile;
            printf("%s\n", [ccProfile.getName UTF8String]);
            // Retrieve info from the object
            // Duplicate cell
            // Change appropriate labels
            // Add cell to a specific section
        }
        // Different types of cells will be added later
        // Concept is the same, what changes is the specific section and cell to duplicate

    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 1){
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reusableShippingCell"];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"reusableShippingCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }

        cell.textLabel.text = @"aldo";

        return cell;
    }



    return nil;
}
// This should be the only part that actually works (I use the same class twice because proxy and shipping are not yet done)
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    // Display profiles
    NSData *arrayData = [[NSUserDefaults standardUserDefaults] objectForKey:@"profileArray"];
    NSArray *profiles = [NSKeyedUnarchiver unarchiveObjectWithData:arrayData];
    NSMutableArray *sectionData = [[NSMutableArray alloc]init];

    for(NSObject *profile in profiles){
        // Credit
        if([profile class] == [CCProfileObject class])
        {
            [sectionData insertObject:@1 atIndex:0];
        }
        // Shipping
        else if([profile class] == [UIImage class])
        {
            [sectionData insertObject:@1 atIndex:1];
        }
        // Proxy
        else if([profile class] == [UIImage class])
        {
            [sectionData insertObject:@1 atIndex:2];
        }
    }

    int toReturn = 0;
    for(int i = 0; i < [sectionData count]; i++){
        toReturn += [[sectionData objectAtIndex:i] integerValue];
    }
    return toReturn;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    // Credit card section
    if(section == 0){
        return 1;
    } else if (section == 1){
        return 2;
    } else {
        return 3;
    }
}
#导入“profileViewController.h”
#导入“./Objs/CCProfileObject.h”
@接口配置文件视图控制器()
@属性(弱、非原子)IBUITableView*tableView;
@属性(强,非原子)IBUITableViewCell*proxySampleCell;
@属性(强,非原子)IBUITableViewCell*ccSampleCell;
@结束
@实现概要视图控制器
@综合桌面视图;
-(无效)viewDidLoad{
//黑暗模式,你可以跳过所有这些
[[NSNotificationCenter defaultCenter]添加观察者:self
选择器:@选择器(更改颜色:)
名称:@“darkToggle”对象:nil];
NSUserDefaults*userDefaults=[NSUserDefaults standardUserDefaults];
if([userDefaults boolforky:@“darkMode”]==是){
[自变色:无];
}
}
-(无效)视图显示:(BOOL)动画{
//显示配置文件
NSData*arrayData=[[NSUserDefaults standardUserDefaults]objectForKey:@“profileArray”];
NSArray*profiles=[NSKeyedUnarchiver unarchiveObjectWithData:arrayData];
用于(配置文件中的NSObject*配置文件){
如果([profile class]==[CCProfileObject class])
{
CCProfileObject*ccProfile=(CCProfileObject*)配置文件;
printf(“%s\n”,[ccProfile.getName UTF8String]);
//从对象中检索信息
//重复单元
//更改适当的标签
//将单元格添加到特定节
}
//稍后将添加不同类型的单元格
//概念是一样的,更改的是要复制的特定部分和单元格
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
if(indexPath.section==1){
UITableViewCell*单元格=[tableView dequeueReusableCellWithIdentifier:@“reusableShippingCell”];
如果(单元格==nil){
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@“reusableShippingCell”];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
}
cell.textlab.text=@“aldo”;
返回单元;
}
返回零;
}
//这应该是唯一真正起作用的部分(我两次使用同一个类,因为代理和传送还没有完成)
-(NSInteger)表格视图中的节数:(UITableView*)表格视图{
//显示配置文件
NSData*arrayData=[[NSUserDefaults standardUserDefaults]objectForKey:@“profileArray”];
NSArray*profiles=[NSKeyedUnarchiver unarchiveObjectWithData:arrayData];
NSMUTABLEARRY*sectionData=[[NSMUTABLEARRY alloc]init];
用于(配置文件中的NSObject*配置文件){
//功劳
如果([profile class]==[CCProfileObject class])
{
[sectionData insertObject:@1索引:0];
}
//船运
else if([profile class]==[UIImage class])
{
[sectionData插入对象:@1索引:1];
}
//代理
else if([profile class]==[UIImage class])
{
[sectionData插入对象:@1索引:2];
}
}
int-toReturn=0;
对于(int i=0;i<[sectionData count];i++){
toReturn+=[[sectionData objectAtIndex:i]integerValue];
}
回归回归;
}
-(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节{
//信用卡组
如果(节==0){
返回1;
}else if(节==1){
返回2;
}否则{
返回3;
}
}

`在-[UITableView]中断言失败\u配置CellForDisplay:forIndexPath:,/BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3698.52.10/UITableView.m:9453
由于未捕获的异常“NSInternalInconsistencyException”,正在终止应用程序,原因:“UITableView()未能从其数据源()获取单元格”
第一次抛出调用堆栈:
(0x1837f6d8c 0x1829b05ec 0x1837f6bf8 0x1841e6fa0 0x18d4975b0 0x18d4941e8 0x18d493e00 0x18d492b1c 0x18d48e668 0x18d3cb770 0x18796d25c 0x1879713ec 0x1878ddaa0 0x1879055d0 0x18D7B6B4 0x18379f2bc 0x18379ea7c 0x18379c7b0 0x1836bcda8 0x18369F020 0x18D69D6978C 0x1048A4DF90 0x183C0)
libc++abi.dylib:以NSException类型的未捕获异常终止
你不需要用一个答案解决我所有的问题。即使是解决问题/解释我需要做的事情也足够了,因为这至少会让我向前迈进一步。`Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit/UIKit-3698.52.10/UITableView.m:9453 Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'UITableView (<UITableView: 0x16106d000; frame = (0 0; 375 812); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x1d0243bd0>; layer = <CALayer: 0x1d0030f60>; contentOffset: {0, -88}; contentSize: {375, 117}; adjustedContentInset: {88, 0, 83, 0}>) failed to obtain a cell from its dataSource (<profileViewController: 0x15ff14c70>) First throw call stack: (0x1837f6d8c 0x1829b05ec 0x1837f6bf8 0x1841e6fa0 0x18d4975b0 0x18d4941e8 0x18d493e00 0x18d492b1c 0x18d48e668 0x18d3cb770 0x18796d25c 0x1879713ec 0x1878ddaa0 0x1879055d0 0x18d7b56b4 0x18379f2bc 0x18379ea7c 0x18379c7b0 0x1836bcda8 0x18569f020 0x18d69d78c 0x1048a4f90 0x18314dfc0) libc++abi.dylib: terminating with uncaught exception of type NSException
UILabel *label = [cell viewWithTag:100];
@implementation ViewController {
    NSArray *data;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // You load your data from NSUserDefault here
    data = [self loadDataFromNSUserDefault];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellIdentifier = @"defaultCell";
    if (indexPath.section == 0) {
        cellIdentifier = @"customCell1";
    } else if (indexPath.section == 1) {
        cellIdentifier = @"customCell2";
    }

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];

    // Access your view's children using their `tag`
    UILabel *title = [cell viewWithTag:100];
    UIImageView *icon = [cell viewWithTag:101];

    // Set content
    title.text = [[data objectAtIndex:indexPath.row] objectForKey:@"title"];
    icon.image = [UIImage imageNamed:[[data objectAtIndex:indexPath.row] objectForKey:@"icon"]];

    return cell;
}