ios - 每个 UIView 的边框

标签 ios objective-c calayer

enter image description here

我必须创建类似于上述图片的内容。我有一个 View ,我需要在吃完 View 后添加边框,例如{早餐、早午餐等。}

为了给出边框,我有以下代码。

CALayer *bottomBorder = [CALayer layer];
bottomBorder.frame = CGRectMake(0.0f, 43.0f, menuHeadView.frame.size.width, 1.0f);
bottomBorder.backgroundColor = [UIColor colorWithWhite:0.8f
                                                 alpha:1.0f].CGColor;
[menuHeadView.layer addSublayer:bottomBorder];

它适用于单个 View ,但如果我有一个用于这些 View 的数组,并且我想在使用 indexPath 的每个 View 之后有边框,它就不起作用,因为它不允许

[array objectAtIndex:i].layer;

请帮忙!提前致谢。

最佳答案

如果您有 View 数组,您可能应该转换: 改变:

[array objectAtIndex:i].layer;

收件人:

((UIView*)[array objectAtIndex:i]).layer;

编辑: 基于 @Mischa 评论:

转换是告诉数组里面有什么对象。如果你在数组中有不同类型的对象(你真的不应该!)或者为了 super 安全你可以改变转换来分配:

id myUnknownObject = [array objectAtIndex:i];
if([myUnknownObject isKindOfClass:[UIView class]]) {
   ((UIView*)myUnknownObject).layer...
} else {
   NSLog(@"%@",[myUnknownObject class]);
}

但是根据您的评论,您的数组中包含 NSStrings,因此您无法将 layer 添加到此对象。请检查您的控制台,它可能是 NSString

当然,因为您提到了 indexPath,并且如果您使用带行的 TableView。您应该使用可重用性和它方法中的好东西:

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    NSDictionary *cellDic = [self.heartbeats objectAtIndex:indexPath.row];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        //here goes reusability part like styling and stuff, this will calling only once for every reusable cell. So for example for cells height 44 this will called only 13 times for iPhone screen.

       //HERE you should manipulate with layer!
    }
    //here goes your custom text of every cell - like "Breakfast", "Brunch" and so on. This will calls for every row which is about to show up. 

      //HERE you should manipulate with text

    return cell;
}

关于ios - 每个 UIView 的边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20902487/

相关文章:

ios - iPhone 应用程序无法启动

ios - 使用 cgImage.mask 屏蔽两个 UIImage 不起作用,但适用于 imageview.layer.mask

ios - Swift 中的 CATransform3DMakeRotation anchor 和位置

ios - 在 iOS 8 + 中获取 UDID

ios - 在ios中获取Json数据

ios - 无法使用 cloudkit 从公共(public)数据库中获取记录

ios - 我应该在哪里准备数据?在 awakeFromNib、viewDidLoad 或其他东西中

ios - ld : library not found for -lstdc++. 6 - Xcode 10

iOS - 如何删除以前添加的 UIView 子层

ios - 涂鸦...Open GL?