2012-10-11 42 views
0

爲什麼setIndentationLevel沒有獲取單元格子視圖? 我有一個UITableView。我的cellForRowAtIndexPath如下所示。我添加的箭頭沒有隨着文本標籤移動。爲什麼?setIndentationLevel不適用於cell.ContentView子視圖

我該怎麼做?

- (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; 
} 
+0

的NSLog( 「%@」,[[[self.tabledata objectAtIndex:indexPath.row] valueForKey:@「level」] intValue]); ....請嘗試一下,看看你的consol會顯示什麼? – 2012-10-11 10:50:20

+0

是的,我試過了。它顯示0,1,2,3等...... – Dev

+0

然後你想爲你的cell.textLabel設置索引級別多少? – 2012-10-11 10:57:48

回答

2

是啊!!!它的工作現在。 我創建自定義單元格和其米文件是這樣

#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; 
} 
相關問題