- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我有一个包含内容的数组。像往常一样,它包含 20 个对象。我想在 Tableview 中将同一个数组分成两部分。我正在尝试在当前数组中使用 NSMake 来实现它。例如,我需要进入第一个 tableview 部分 3 行,第二个将包含所有其余部分(17 行)。
switch (section) {
case 0:
return
[[array subarrayWithRange:NSMakeRange(3, 8)] count];
// in this line, it always takes from the first object in array, despite I told hime start from 3 (If I understand right, how to works NSMakeRange)
break;
case 1:
return
[[array subarrayWithRange:NSMakeRange(9, 19)] count];
// here my app is crashing with an error
//*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSArray subarrayWithRange:]: range {9, 19} extends beyond bounds [0 .. 19]'
default:
break;
}
有人可以帮我吗?
最佳答案
NSMakeRange
被定义为 (startingIndex, length)
,而不是 (start, end)
这看起来像是您尝试使用的方式
因此,如果您需要前 3 个对象,那么其余的对象将如下所示:
switch (section) {
case 0:
// This returns objects 0-2 in the array
return [array subarrayWithRange:NSMakeRange(0, 3)];
case 1:
// This returns objects 3-20 in the array
return [array subarrayWithRange:NSMakeRange(3, 17)];
default:
break;
}
编辑:根据您的评论,您实际上是在寻找要返回的部分中行数的计数。由于您使用的是固定行数,因此您可以在 case 语句中返回实际行数。
switch (section) {
case 0:
// This returns the count for objects 0-2 in the array
return 3;
case 1:
// This returns the count for objects 3-20 in the array
return 17;
default:
break;
}
您实际上不需要使用[subarrayWithRange]
,也不需要使用NSMakeRange
。如果你确实需要在某个时候引用实际的数组,你将得到一个 NSIndexPath 对象,你可以使用它从你的数组中获取对象。您将需要使用 section 和 row 属性。
编辑:NSRange
-> NSMakeRange
关于ios - 如何使用 NSRange 创建 NSArray 的子数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19597034/
如果案例 1:range1 是 (5,10) 并且 range2 是 (8,0) 它应该返回 true。情况 2:range1 是 (5,10) 并且 range2 是 (5,4) 它应该返回 tru
示例代码: import XCTest func == (lhs: NSRange, rhs: NSRange) -> Bool { return lhs.location == rhs.lo
我正在尝试实现 Easy Game Center link但我得到这个错误: Cannot convert value of type 'NSRange' (aka 'NSRange') to exp
我试图在一个字符串中查找多个字符串的 NSRange。 在下面的代码中,我使用 String.range(of: String, options: , Range) 方法找到 Range,并转换为 N
我在使用 NSRange 时遇到问题。这是我的代码: NSRange range = [[[NSHTTPCookie requestHeaderFieldsWithCookies:[[NSHTTPCo
我看了一段关于 ios7 编程的视频,看到了下面的一段代码。 我真的不能理解变量范围在这里是如何工作的。 我们创建了一个 NSrange 变量范围并且从不给它赋值,所以我认为 &range 指针应该返
我的 NSRange 有一个问题,导致我的应用程序在 textview 为空时崩溃。我使用的是自定义键盘,上面有一个退格键,它在此处调用此代码... if ([self.myChart isFirst
我正在开发一个应用程序,它接受一种名为“泰米尔语”的语言的输入。因此,为了找到字符串中任何特定字符的范围,我使用了以下代码。 var range = originalWord.rangeOfStrin
我的设备 (iPhone iOS 4.3) 上有奇怪的 NSRange 行为。此代码在设备和模拟器上有不同的行为。 for (Location *location in locationArray)
我写了一个在地址簿中搜索人的方法,我希望即使调用方法[someAdressBook searchName:@"joh"];也能找到“john bigs”;. 我的方法适用于全名,但我在部分名称中遇到问
我编写了一种方法,通过向它发送该词的 NSString 来突出显示段落中的词,在我遇到这种情况之前,它一直运行良好: 当我有这段文字时: Their mother has tried dressing
我遇到了这种奇怪的效果,在 rangeOfString 上调用 location 会返回一个非常大的值,而它不应该... 我的代码是这样的: NSRange range = [string range
这个问题在这里已经有了答案: What does NSMakeRange(i, 1) mean? (2 个答案) 关闭 6 年前。 我正在使用带有属性字符串的 NSRange,但我不完全理解文本的哪
NSRange 变量是否可能包含多个范围?像这样的东西: var multipleRanges: NSRange = [NSMakeRange(0, 2), NSMakeRange(10, 1),
我有一个字符串: He said "hello mate" yesterday. 我想获取从第一个引文到最后一个引文的 NSRange。所以我尝试了这样的事情: NSRange openingRang
我正在努力尝试生成一个有效的 NSRange,它似乎不应该这么复杂,所以我猜我使用了错误的方法。这是我正在尝试做的事情: 我有一个字符串,其中包含一些 unicode 字符: "The quick b
我有一个返回 NSRange 的方法。当我从类外部调用此方法时,出现编译错误。 NSRange tmpRange; tmpRange = [phrase rangeInString:searchStr
我有一个从 http GET 返回的大字符串,我正在尝试确定它是否有特定的文本片段(请原谅我的错误) 我的问题是:我可以/应该使用 NSRange 来确定这段文本是否存在吗? NSRange te
这是我的代码: regex.enumerateMatchesInString(parsingMemo.string, options: NSMatchingOptions(rawValue: 0),
这是我的代码:我正在尝试格式化我的文本 NSMutableAttributedString(),但它似乎总是超出范围。 Terminating app due to uncaught exceptio
我是一名优秀的程序员,十分优秀!