Iphone UITableViewCell viewWithTag获取UIView故障帧
我在故事板中的Iphone UITableViewCell viewWithTag获取UIView故障帧,iphone,ios,uitableview,custom-cell,Iphone,Ios,Uitableview,Custom Cell,我在故事板中的UITableView中的自定义单元格中添加了一个UIView,并按如下方式访问它: UIView *customView = (UIView *)[cell viewWithTag:CELL_CUSTOM_VIEW_TAG]; NSLog(@"%f", customView.frame.size.width); 但是我得到了它的宽度0.0000。我是否以错误的方式访问它 编辑: 以下是我的完整cellForRow方法: - (UITableViewCell *)tableVie
UITableView
中的自定义单元格中添加了一个UIView
,并按如下方式访问它:
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;
}
我不明白你的逻辑。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);
}
}
您在哪里设置了uiview的框架?@AlickDikan是否检查了
customView
是否为nil
?可能标签设置不正确?您何时调用此代码?当时单元格的大小是多少?我在cellForRowAtIndexPath中调用此代码,此时单元格的帧是320x131,自定义视图不是nill。向我们显示-cellForRowAtIndexPath
的代码。我这样做是因为我在故事板中有一个自定义的UITableViewCell,例如,我在其中添加了一个UIButton(因为我只是不想以编程的方式进行)。我通过使用cell viewWithTag检索它。这是我的逻辑。为了好玩,请尝试添加UIView或UIButton(无论您尝试添加什么)以编程方式添加到表中。我打赌你会找到它的。相信我,我以前也走过这条路。没有必要以编程方式添加UIView
。向我们展示代码,我认为你犯了一个小错误,我们可以指出你的错误。只是一个小提示,而不是直接将内容添加到UITableViewCell中,你可能希望在o改为使用contentView属性。