gpt4 book ai didi

ios - 设置 UITableViewCell 控件数据时应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 03:05:06 25 4
gpt4 key购买 nike

我有一个应用程序,我按如下方式在 viewDidLoad 中加载数据

 dispatch_queue_t concurrentQueue = dispatch_queue_create("MyQueue", NULL);
dispatch_async(concurrentQueue, ^{
[self loadProfile];
dispatch_async(dispatch_get_main_queue(), ^{
[tblViewProfile reloadData];

});
});

自定义UITableViewCell中标签控件创建cell并设置数据后应用崩溃,代码如下

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.row == 0)
{
static NSString *simpleTableIdentifier = @"ProfileViewCell";

ProfileViewCell *cell = (ProfileViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray * customcellArray = [[NSBundle mainBundle]loadNibNamed:@"ProfileViewCell" owner:self options:nil];
for(id customcellObject in customcellArray){
if([customcellObject isKindOfClass: [UITableViewCell class]]){
cell = (ProfileViewCell *)customcellObject;
break;
}
}

}
cell.lblFollowers.text = strFollowers;
cell.lblFollowing.text = strFollowing;
cell.lblName.text = strName;
cell.lblOOTD.text = strOOTD;

return cell;
}
else
{
static NSString *simpleTableIdentifier = @"PostViewCell";


PostViewCell *cell = (PostViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil)
{
NSArray * customcellArray = [[NSBundle mainBundle]loadNibNamed:@"PostViewCell" owner:self options:nil];
for(id customcellObject in customcellArray){
if([customcellObject isKindOfClass: [UITableViewCell class]]){
cell = (PostViewCell *)customcellObject;
break;
}
}

}



return cell;


}

}

当我在这里给小组成员设置数据时

 cell.lblFollowers.text = strFollowers;
cell.lblFollowing.text = strFollowing;
cell.lblName.text = strName;
cell.lblOOTD.text = strOOTD;

上面的第一行导致应用程序崩溃并出现访问错误。如果我删除那一行,下一行会导致同样的崩溃,我不知道这次崩溃的原因。

请帮忙

编辑做了一些测试,然后发现 Bad Exec 是由strFollowing、strFollowers 等,但我已经初始化了它们,但不确定它们在哪里以及为什么它们会被释放。

最佳答案

您可以创建一个 Utility 方法来从 NibName 获取 Class 实例

+ (id)loadNibNamed:(NSString *)nibName ofClass:(Class)objClass 
{
if (nibName && objClass)
{
NSArray *objects = [[NSBundle mainBundle] loadNibNamed:nibName
owner:nil
options:nil];
for (id currentObject in objects )
{
if ([currentObject isKindOfClass:objClass])
return currentObject;
}
}
return nil;
}

像这样在你的代码中使用它

ProfileViewCell *cell = (ProfileViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ProfileViewCell"];
if (cell == nil)
{
cell = [Utility loadNibNamed:@"ProfileViewCell" ofClass:[ProfileViewCell class]];

}

PostViewCell *cell = (PostViewCell *)[tableView dequeueReusableCellWithIdentifier:@"PostViewCell"];
if (cell == nil)
{
cell = [Utility loadNibNamed:@"PostViewCell" ofClass:[PostViewCell class]];

}

希望这对你有帮助。

关于ios - 设置 UITableViewCell 控件数据时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22868792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com