kvc获取子视图lastobject

时间:2015-09-18 08:39:46

标签: ios subview kvc

我有问题。 首先,我有一个像这样的NSArray:

(NSMutableArray *)currentCanMoveAndDeleteCellArray {
        NSInteger count = [self.contentCollectionView numberOfItemsInSection:0];
        NSMutableArray *currentCells = [NSMutableArray arrayWithCapacity:count];
        for (int index = 0 ; index < count; ++index) {
            UICollectionViewCell *cell = [self.contentCollectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
            if(cell) {
                [currentCells addObject:[self.contentCollectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]]];
            }
        }
        return currentCells;
    }

然后我想得到他们的最后一个子视图,我这样使用kvc get subviews

[self.currentCanMoveAndDeleteCellArray valueForKeyPath:@"subviews"]

我得到了:

    <__NSArrayI 0x1710793c0>(
    <__NSArrayM 0x17405a670>(
    <UIView: 0x155de9020; frame = (0 0; 80 30); gestureRecognizers = <NSArray: 0x170248f40>; layer = <CALayer: 0x1702363a0>>,
    <UILabel: 0x155e524d0; frame = (70 -10; 20 20); clipsToBounds = YES; tag = 123; gestureRecognizers = <NSArray: 0x17405a520>; layer = <_UILabelLayer: 0x17409f540>>
    )
,
 <__NSArrayM 0x17405cf80>(
    <UIView: 0x155e7cbf0; frame = (0 0; 80 30); gestureRecognizers = <NSArray: 0x17405d3a0>; layer = <CALayer: 0x174431a40>>,
    <UILabel: 0x155e7ceb0; frame = (70 -10; 20 20); clipsToBounds = YES; tag = 123; gestureRecognizers = <NSArray: 0x17405d400>; layer = <_UILabelLayer: 0x17409f9a0>>
    )
        ,
 <__NSArrayM 0x17405f6b0>(
    <UIView: 0x155e7da80; frame = (0 0; 80 30); gestureRecognizers = <NSArray: 0x17405faa0>; layer = <CALayer: 0x174433440>>,
    <UILabel: 0x155e7dd40; frame = (70 -10; 20 20); clipsToBounds = YES; tag = 123; gestureRecognizers = <NSArray: 0x17405fb00>; layer = <_UILabelLayer: 0x174282f30>>
    )
<__NSArrayM 0x174240990>(
<UIView: 0x155e7e150; frame = (0 0; 80 30); gestureRecognizers = <NSArray: 0x174240d80>; layer = <CALayer: 0x174433e60>>,
<UILabel: 0x155e7e410; frame = (70 -10; 20 20); clipsToBounds = YES; tag = 123; gestureRecognizers = <NSArray: 0x174240de0>; layer = <_UILabelLayer: 0x174283610>>
)
,
    <__NSArrayM 0x174241c50>(
    <UIView: 0x155e7ec30; frame = (0 0; 80 30); gestureRecognizers = <NSArray: 0x174242040>; layer = <CALayer: 0x174434880>>,
    <UILabel: 0x155e7eef0; frame = (70 -10; 20 20); clipsToBounds = YES; tag = 123; gestureRecognizers = <NSArray: 0x1742420a0>; layer = <_UILabelLayer: 0x174283cf0>>
    )
,
<__NSArrayM 0x174242f10>(
<UIView: 0x155e7f710; frame = (0 0; 80 30); gestureRecognizers = <NSArray: 0x174243300>; layer = <CALayer: 0x1744352a0>>,
<UILabel: 0x155e7f9d0; frame = (70 -10; 20 20); clipsToBounds = YES; tag = 123; gestureRecognizers = <NSArray: 0x174243360>; layer = <_UILabelLayer: 0x1742843d0>>
)
)

谁能告诉我如何获得最后一个子视图。

我试着写:  [self.currentCanMoveAndDeleteCellArray valueForKeyPath:@"subviews.@lastObject"]

但是,你知道这不是我想要的结果。

1 个答案:

答案 0 :(得分:0)

您应该在数组中使用UICollectionViewCell类。 之后,您可以获得子视图。

- (NSMutableArray *)currentCanMoveAndDeleteCellArray {
    NSInteger count = [self.contentCollectionView numberOfItemsInSection:0];
    self.currentCells = [NSMutableArray arrayWithCapacity:count];

    for (int index = 0 ; index < count; ++index) {

        UICollectionViewCell *cell = [self.contentCollectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];

        if(cell) {

            [ self.currentCells addObject:cell]; }

    } return  self.currentCells;

}
-(void)getCell{

    UICollectionViewCell *cell = [ self.currentCells objectAtIndex:_index];

    for (UIView * view in [cell subviews]) {
        NSLog(@"%@",view);
    }
}