从委托数组中获取所有对象属性

发布于 2024-11-07 04:37:00 字数 601 浏览 0 评论 0原文

我成功地从我的委托中检索了一个数组,但是我正在努力获取所有对象属性,因此在我的 AppDelegate 中:

    arrayOne = [[NSMutableArray alloc] init];
    NSMutableArray *tempArray1 = [self myArray];
// Add names to arrayOne
    for (MyInfo *info in tempArray1) {
        [arrayOne addObject:info.name];
    }

然后在我的 MainView 中检索它

    cell.textLabel.text = [delegate.currentlyUsedArray objectAtIndex:row];

:工作正常,但 myArray 包含其他属性,例如:info.ageinfo.height — 如何将这些属性获取到另一个 文本标签?我必须采用与上述相同的方法还是有更有效的方法?

I am successfully retrieving an array from my delegate, however I am struggling to get all of the objects properties, so in my AppDelegate:

    arrayOne = [[NSMutableArray alloc] init];
    NSMutableArray *tempArray1 = [self myArray];
// Add names to arrayOne
    for (MyInfo *info in tempArray1) {
        [arrayOne addObject:info.name];
    }

I then retrieve this in my MainView:

    cell.textLabel.text = [delegate.currentlyUsedArray objectAtIndex:row];

This works fine, but myArray contains other properties such as: info.age and info.height — how do I get these to another textLabel? Do I have to do the same approach as above or is there a more efficeint way?

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

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

发布评论

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

评论(3

如痴如狂 2024-11-14 04:37:00

为什么不能直接将 info 添加到数组中。

for (MyInfo *info in tempArray1) {
    [arrayOne addObject:info];
}

以及稍后您要设置它的位置。

MyInfo *info = (MyInfo*)[delegate.currentlyUsedArray objectAtIndex:row];
cell.textLabel.text = info.name;

// Other fields should also accessible directly such as info.age and info.height.

Why can't you just add info to the array.

for (MyInfo *info in tempArray1) {
    [arrayOne addObject:info];
}

and later where you are setting it.

MyInfo *info = (MyInfo*)[delegate.currentlyUsedArray objectAtIndex:row];
cell.textLabel.text = info.name;

// Other fields should also accessible directly such as info.age and info.height.
刘备忘录 2024-11-14 04:37:00

混合泳,
您可以将 appDelegate myArray 的方法公开,并从您需要的地方获取信息值,在本例中是在 cellForRowAtIndexPath: 方法中。

medley,
You can make method of appDelegate myArray public and get your info values right from where you need it, in this case in cellForRowAtIndexPath: method.

月依秋水 2024-11-14 04:37:00

按照 Zapko 的回答,MainView 中的代码将如下所示,以访问委托对象的 myArray 属性中的 MyInfo 对象的各种属性:

cell.textLabel.text  = [(MyInfo *)[delegate.myArray objectAtIndex:row] name];
cell.ageLabel.text   = [(MyInfo *)[delegate.myArray objectAtIndex:row] age];
cell.heigtLabel.text = [(MyInfo *)[delegate.myArray objectAtIndex:row] height];

Following Zapko's answer, your code in MainView would look like this to access the various properties of a MyInfo object in the myArray property of your delegate object:

cell.textLabel.text  = [(MyInfo *)[delegate.myArray objectAtIndex:row] name];
cell.ageLabel.text   = [(MyInfo *)[delegate.myArray objectAtIndex:row] age];
cell.heigtLabel.text = [(MyInfo *)[delegate.myArray objectAtIndex:row] height];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文