作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为什么 setIndentationLevel
没有获取单元格 subview ?我有一个 UITableView
。我的 cellForRowAtIndexPath
如下所示。我添加的箭头没有随 textlabel 移动。为什么?
我该怎么做?
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"small_arrow.png"]];
img.frame = CGRectMake(5, 10, 7, 11);
[cell.contentView addSubview:img];
cell.textLabel.text=[[self.tabledata objectAtIndex:indexPath.row] valueForKey:@"name"];
cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:18];
[cell setIndentationLevel:[[[self.tabledata objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]];
return cell;
}
最佳答案
是啊!!!它现在正在工作。我创建了一个自定义单元格,它的 m 文件就像
#import "customecell.h"
@implementation customecell
@synthesize textLbl,img;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
textLbl = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, 320, 20)];
textLbl.backgroundColor = [UIColor clearColor];
[self.contentView addSubview:textLbl];
img = [[UIImageView alloc]initWithFrame:CGRectMake(5, 10, 7, 11)];
img.image = [UIImage imageNamed:@"small_arrow.png"];
[self.contentView addSubview:img];
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)layoutSubviews{
[super layoutSubviews];
float indentPoints = self.indentationLevel * self.indentationWidth;
self.contentView.frame = CGRectMake(
indentPoints,
self.contentView.frame.origin.y,
self.contentView.frame.size.width - indentPoints,
self.contentView.frame.size.height
);
}
@end
并且 cellForRowAtIndexPath
更改为
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifier = @"Cell";
customecell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[customecell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLbl.text=[[self.tabledata objectAtIndex:indexPath.row] valueForKey:@"name"];
cell.textLbl.font = [UIFont fontWithName:@"Helvetica" size:18];
[cell setIndentationLevel:[[[self.tabledata objectAtIndex:indexPath.row] valueForKey:@"level"] intValue]];
cell.indentationWidth = 25;
float indentPoints = cell.indentationLevel * cell.indentationWidth;
cell.contentView.frame = CGRectMake(indentPoints,cell.contentView.frame.origin.y,cell.contentView.frame.size.width - indentPoints,cell.contentView.frame.size.height);
return cell;
}
关于iphone - setIndentationLevel 不适用于 cell.ContentView subview ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12837613/
我是一名优秀的程序员,十分优秀!