2011-08-24 76 views
0

我有一個自定義UITableViewCell類,並希望線性顯示圖像和字符串。例如:在子類UITableViewCell內動態創建子視圖

第1列:[IMAGE1]字符串1 [圖像2]字符串2 [的Image3]

第2列:[圖像4] STRING3 [圖像5]

這些圖像具有不同的寬度,但我想要等間距。我將如何做到這一點?我試圖操縱子視圖和CGRectMake無濟於事。

此外,我使用NSDictionary來保存內容,並且每個單元格的圖像/字符串數量不是固定的。

CustomCell類:

#import "CustomCell.h" 


@implementation CustomCell 

@synthesize primaryLabel,secondaryLabel,image1; 


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { 
    if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) { 
     // Initialization code 
     primaryLabel = [[UILabel alloc]init]; 
     primaryLabel.textAlignment = UITextAlignmentLeft; 
     primaryLabel.font = [UIFont systemFontOfSize:16]; 
     secondaryLabel = [[UILabel alloc]init]; 
     secondaryLabel.textAlignment = UITextAlignmentLeft; 
     secondaryLabel.font = [UIFont systemFontOfSize:14]; 
     image1 = [[UIImageView alloc]init]; 

     [self.contentView addSubview:primaryLabel]; 
     [self.contentView addSubview:secondaryLabel]; 
     [self.contentView addSubview:image1]; 

    } 
    return self; 
} 

- (void)layoutSubviews { 
    [super layoutSubviews]; 
    CGRect frame; 
    frame= CGRectMake(0 ,5, 200, 25); 
    primaryLabel.frame = frame; 

    frame= CGRectMake(0 ,30, 200, 25); 
    secondaryLabel.frame = frame; 

    frame= CGRectMake(0, 60, 23, 20); 
    image1.frame = frame; 

...

RootViewController

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

    static NSString *CellIdentifier = @"Cell"; 

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
     cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    } 

    // Set up the cell... 
    NSDictionary *dictionary = nil; 


//Search 
    if (tableView == self.searchDisplayController.searchResultsTableView) 
    { 
     dictionary = [self.filteredListContent objectAtIndex:indexPath.row]; 
    } 
    else 
    { 
     dictionary = [self.tableDataSource objectAtIndex:indexPath.row]; 
    } 


//Original 
    cell.primaryLabel.text = [dictionary objectForKey:@"Title"];  


    for (NSArray *keystroke in [dictionary objectForKey:@"Strokes"]) { 


      for (int i = 0; i < 2; i++) { 


       if ([(NSString *)keystroke isEqualToString:@"string1"] || [(NSString *)keystroke isEqualToString:@"string2"]) { 
       cell.secondaryLabel.text = (NSString *)keystroke; 
       } 

       else { 
      NSString *imageFilePath = [NSString stringWithFormat:@"%@.png", keystroke]; 
      NSLog(@"%@", imageFilePath); 
      UIImage *myimage = [UIImage imageNamed:imageFilePath]; 

       cell.image1.image = myimage; 

     } 
     } 
    } 
    return cell; 


} 

...

顯然有很多孔的位置。首先,當我循環訪問字典時,我需要將子視圖的CustomCell右移,以便將圖像/文本放置在上一個子視圖旁邊。

回答

0

您正處於正確的軌道上。在您的UITableViewCell子類中,您需要手動覆蓋layoutSubviews,爲每個UIImageViewUILabel定義CGRects,並將它們設置爲相應視圖的框架。

結賬CGRectGetMaxX。在這方面它將非常有用。

+0

我已經添加了一些代碼,因爲我仍然丟失。謝謝回覆! –