2015-11-24 16 views
0

我在我的tableViewCell中加载了一些UIView。其中一个属性是边框颜色。这些颜色存储在NSArray,这是buttonColorArray。我想设置他们在我的cellForRowAtIndexPath这样的:当颜色来自NSArray时,如何在tableViewCell中设置多个UIView的边框颜色

ViewDidLoad

self.buttonColorArray = [[[NSArray alloc] initWithObjects: 
           [UIColor colorWithRed:250.0f/255.0f green:110.0f/255.0f blue:40.0f/255.0f alpha:1.0f], 
           [UIColor colorWithRed:77.0f/255.0f green:212.0f/255.0f blue:92.0f/255.0f alpha:1.0f], 
           [UIColor colorWithRed:255.0f/255.0f green:197.0f/255.0f blue:0.0f/255.0f alpha:1.0f], 
           [UIColor colorWithRed:59.0f/255.0f green:89.0f/255.0f blue:152.0f/255.0f alpha:1.0f], 
           [UIColor colorWithRed:84.0f/255.0f green:171.0f/255.0f blue:237.0f/255.0f alpha:1.0f], nil] autorelease]; 

cellForRowAtIndexPath

[Cell.expandedCellContainerView.layer setBorderColor:[self.buttonColorArray objectAtIndex:indexPath.row]]; 

但是我在这里得到一个警告。据我所知边框颜色总是被设置为.CGColor。所以我加我的方法是这样的:

[Cell.expandedCellContainerView.layer setBorderColor:[self.buttonColorArray objectAtIndex:indexPath.row].CGColor]; 

但它不工作。 (这是工作,如果我只是在这里添加一个固定的颜色,而不是阵列)

我也尝试添加.CGColor在我的buttonColorArray。但它仍然不起作用。如果有人知道使用它的正确方法和理由,请与我分享。

很多预先感谢。

回答

1

UIColor有一个属性CGColor。只需访问它,并把它给你的看法边框颜色是这样的:

UIColor * mycolor = [UIColor whiteColor]; 

[view.layer setBorderColor:mycolor.CGColor]; 
0

您需要设置cell.view.layer.borderWidth属性与颜色,使其工作一起。 这里的cellForRowAtIndexPath

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

static NSString *reuse = @"cell"; 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse]; 
if(cell == nil){ 
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse]; 
} 

UIView *v = [[UIView alloc]initWithFrame:CGRectMake(50, 0, 100, 44)]; 
v.backgroundColor = [UIColor grayColor]; 

UIColor *currentColor = arrayOfColors[indexPath.row]; 
v.layer.borderColor = currentColor.CGColor; 
v.layer.borderWidth = 1; // You need to add this line accordingly 
[cell addSubview:v]; 

return cell; 

} 
0

一个例子试试这个

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 
    [cell.contentView.layer setBorderColor:[self.buttonColorArray objectAtIndex:indexPath.row].CGColor]; 
    [cell.contentView.layer setBorderWidth:2.0f]; 
} 
0

layer.borderWidth缺省值为零,你可能忘了设置。