gpt4 book ai didi

ios - [myView isKindOfClass :[UIView class]] giving weird result

转载 作者:行者123 更新时间:2023-11-28 18:18:29 26 4
gpt4 key购买 nike

我有 UITableViewCell,我在其中添加一个 UIView 并在其中添加 subview ,如 UIButtonUIImageView

现在我想访问 willDisplayCell 中的 UIView。为此,我在下面。

NSArray *subviews;
subviews = cell.subviews;

int xxyyzz = 1;
for(id aView in subviews) {
NSLog(@"aView====%@" , [UIView class]);
if([aView isKindOfClass:[UIView class]]) {
NSLog(@"This is UIView... Do what you want to do...");
}
}

但是当我运行时,我得到 This is UIView... Do what you want to do... 用于所有 subview (即 UIButton、UIImageView 等)。 aView====UIView

我相信这是因为 UIView 是所有对象的父类(super class)。

有没有办法让我只能访问 UITableViewCell 中的 UIView...

最佳答案

我没有完全理解你的问题,但可能在使用方法 -isKindOfClass 时存在误解。特别是该方法对类和子类响应 YES,UIButtonUITableView 等是 UIView 的子类,因此该方法按预期工作,这里是文档:

Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class.

您很可能想使用 -isMemberOfClass,它只考虑对象是否是特定类的实例。
如果您正在检查的对象来自类集群,例如 NSArray,您还必须注意使用该方法,引用 Apple 文档:

Be careful when using this method on objects represented by a class cluster. Because of the nature of class clusters, the object you get back may not always be the type you expected. If you call a method that returns a class cluster, the exact type returned by the method is the best indicator of what you can do with that object. For example, if a method returns a pointer to an NSArray object, you should not use this method to see if the array is mutable, as shown in the following code:

// DO NOT DO THIS! if ([myArray isKindOfClass:[NSMutableArray class]])
{
// Modify the object
}

If you use such constructs in your code, you might think it is alright to modify an object that in reality should not be modified. Doing so might then create problems for other code that expected the object to remain unchanged.

If the receiver is a class object, this method returns YES if aClass is a Class object of the same type, NO otherwise.

关于ios - [myView isKindOfClass :[UIView class]] giving weird result,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26817197/

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