gpt4 book ai didi

iOS titleForHeaderInSection 崩溃

转载 作者:行者123 更新时间:2023-11-28 19:21:07 25 4
gpt4 key购买 nike

问题是这两个部分有时是空的,所以我不知道当它们变空时如何防止崩溃。 self.globalSections 是一个全局 NSMutableArrays,由本地两个 NSMutableArrays 存储,这两个 NSMutableArrays 是我的 UITableView 的第一部分和第二部分。

这是我的代码:

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section 
{

if (mySearchBar.text.length > 0)
{
if(section == 0)
{
//below are if statements I have tried, but not working...
//NSUInteger globalSectionsOne = [self.globalSections indexOfObject:[self.globalSections objectAtIndex:0]];
//if ([[self.globalSections objectAtIndex:0]count] >=1)
// if (globalSectionsOne!=NSNotFound)
//if (globalSectionsOne >= 1)

//{
NSLog(@"sections counts");
NSUInteger firstSectionCount = [[self.globalSections objectAtIndex:0]count];

NSString *firstSectionString = [NSString stringWithFormat:@"%i Exact match(es)", firstSectionCount];

return firstSectionString;
//}
//else {return @"";}

}
else if (section == 1)
{
//another same code, except the objectAtIndex is at 1 instead of 0.

请指出正确的方向,谢谢。

编辑:

我在这个方法中做了修改:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

if (mySearchBar.text.length > 0 && [self.globalSections count] == 2)
{
return 2;
}
else
{
return 1;
}

//return 1;
}

这样可以吗?

最佳答案

您可以这样避免在 titleForHeaderInSection: 中重复代码:

if (mySearchBar.text.length > 0)
{
if ([self.globalSections count] > section)
{
int sectionCount = [[self.globalSections objectAtIndex:section] count];
NSString *matchWord = (sectionCount == 1) ? @"match" : @"matches";
return [NSString stringWithFormat:@"%i Exact %@", sectionCount, matchWord];
}
else
{
return @"";
}
}

关于iOS titleForHeaderInSection 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8733527/

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