0

我在情节提要UIView的自定义单元格中添加了一个,UITableView然后像这样访问它:

UIView *customView = (UIView *)[cell viewWithTag:CELL_CUSTOM_VIEW_TAG];
NSLog(@"%f", customView.frame.size.width);

但我知道它的宽度0.0000。我是否以错误的方式访问它?

编辑:这是我的完整 cellForRow 方法:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CustomCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    UIImageView *customImageView = (UIImageView *)[cell viewWithTag:CELL_IMAGE_VIEW_TAG];
    customImageView.image = [UIImage imageNamed:@"foo.jpg"];
    //down is the code why I need the frame of it
    //I'm trying to make a right border to this UIImageView:
    CGRect imageViewFrame = customImageView.frame;
    CALayer *imageViewRightBorder = [self rightBorderWithColour:[UIColor blackColor] forView:imageViewFrame];
    [customImageView.layer addSublayer:imageViewRightBorder];

    return cell;
}

这是我为 imageView 设置右边框的方法:

- (CALayer *)rightBorderWithColour:(UIColor *)color forView:(CGRect)viewFrame {
    CALayer *rightBorder = [CALayer layer];
    rightBorder.frame = CGRectMake(viewFrame.size.width, 0.0f, 1.0f, viewFrame.size.height);
    rightBorder.backgroundColor = [color colorWithAlphaComponent:1.0f].CGColor;
    return rightBorder;
}
4

1 回答 1

2

我不明白你的逻辑。cellForRowAtIndexPath 是您定义 UIView 的地方,因此您拥有它。正如其他用户所说,向我们展示您的代码。

无论如何,这就是我在我的 tableview 中定义一个自定义 UIButton 的方式,它显示在每一行中。我假设您正在尝试做类似的事情。忘记将您的 UIView 添加到情节提要。看看您是否可以使用此代码复制它。当然将其更改为您的 UIView 代码。

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

NSString *CellIdentifier = [NSString stringWithFormat:@"S%1dR%1d",indexPath.section,indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell == nil)
    {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button addTarget:self
                   action:@selector(timerStopStart:)
         forControlEvents:UIControlEventTouchDown];
        [button setTitle:@"Start/Stop" forState:UIControlStateNormal];
        button.frame = CGRectMake(5.0f, 40.0f, 72.0f, 77.0f);
        button.tag = (indexPath.row)+100;
        [cell addSubview:button];

        //NSLog(@"number: %d ...", (indexPath.row)+100);
    }
}
于 2013-09-02T13:28:21.327 回答