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
右移,以便將圖像/文本放置在上一個子視圖旁邊。
我已經添加了一些代碼,因爲我仍然丟失。謝謝回覆! –