Objective C 通过 cellForRowAtIndexPath 使用 2D 数组

发布于 2024-11-08 11:18:37 字数 1270 浏览 0 评论 0原文

我知道使用 indexPath 和 row 将数组与 cellForRowAtIndexPath 一起使用的基本思想,但我不确定浏览 2D 数组的最佳方法。我创建了一个自定义单元格,将 7 个不同的字符串接受到 7 个支架中,并且创建了一个包含 3 行、每行 7 个字符串的 2D 数组。 (3x7 二维数组)。关于如何设置 cellForRowAtIndexPath 以自动遍历此数组有什么建议吗?

让我更详细地介绍一下。我的多维数组是一个包含 7 个字符串的数组: ("Potato", "0", "1", "2", "3", "4", "5") 我的自定义单元格已全部设置完毕有 7 个子视图(它们有效,我用数组测试了它)。在我创建自定义单元格的同一个类中,我创建了一个方法,将测试数组中的值分配给这些子视图:

- (void)setMyArray:(NSArray *)myArray
{
   NSString *name = [myArray objectAtIndex:0];
   [nameLabel setText:name];
   [name release];
   NSString *numberA = [myArray objectAtIndex:1];
   [aLabel setText:numberA];
   [numberA release];
   NSString *numberB = [myArray objectAtIndex:2];
   [bLabel setText:numberB];
   [numberB release];
   NSString *numberC = [myArray objectAtIndex:3];
   [cLabel setText:numberC];
   [numberC release];
   NSString *numberD = [myArray objectAtIndex:4];
   [dLabel setText:numberD];
   [numberD release];
   NSString *numberE = [myArray objectAtIndex:5];
   [eLabel setText:numberE];
   [numberE release];
   NSString *numberF = [myArray objectAtIndex:6];
   [fLabel setText:numberF];
   [numberF release];
}

在 vi​​ewController 中,我必须调用的是:

[cell setMyArray:testList];

它可以工作。我只是不确定如何使用二维数组来做到这一点。

I know the basic idea of using arrays with cellForRowAtIndexPath using indexPath and row, but I'm unsure of the best way to go through a 2D array. I've created a custom cell that accepts 7 different strings into 7 holders and I've created a 2D array that has 3 rows of 7 strings. (3x7 2D array). Any suggestions on how to set up the cellForRowAtIndexPath to automatically go through this array?

Let me go into more detail. My multidimensional-array is an array of arrays that contain 7 strings: ("Potato", "0", "1", "2", "3", "4", "5") My custom cell is all set up with 7 subviews (they work, I tested it with an array). In the same class that I create the custom cell, I've created a method that assigns values from the test array to those subviews:

- (void)setMyArray:(NSArray *)myArray
{
   NSString *name = [myArray objectAtIndex:0];
   [nameLabel setText:name];
   [name release];
   NSString *numberA = [myArray objectAtIndex:1];
   [aLabel setText:numberA];
   [numberA release];
   NSString *numberB = [myArray objectAtIndex:2];
   [bLabel setText:numberB];
   [numberB release];
   NSString *numberC = [myArray objectAtIndex:3];
   [cLabel setText:numberC];
   [numberC release];
   NSString *numberD = [myArray objectAtIndex:4];
   [dLabel setText:numberD];
   [numberD release];
   NSString *numberE = [myArray objectAtIndex:5];
   [eLabel setText:numberE];
   [numberE release];
   NSString *numberF = [myArray objectAtIndex:6];
   [fLabel setText:numberF];
   [numberF release];
}

In the viewController, all I have to call is:

[cell setMyArray:testList];

And it works. I'm just not sure how to do it with a 2D array.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

花辞树 2024-11-15 11:18:37

我不知道你的自定义单元代码到底是什么样子,但你不会这样做吗:

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

    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
    if (cell == nil) {
       //...
    }

    [cell setMyArray:[self.myTwoDArray objectAtIndex:indexPath.row]];

    // and so on...

另外,Rob 关于你的内存管理的说法是正确的。当您将对象从原始数组中取出时,不要释放它们。

I don't know what exactly your custom cell code looks like, but wouldn't you just do:

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

    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
    if (cell == nil) {
       //...
    }

    [cell setMyArray:[self.myTwoDArray objectAtIndex:indexPath.row]];

    // and so on...

Also, Rob's right about your memory management. Don't release the objects as you take them out of the original array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文