Tôi tạm thời thêm một số giá trị vào UITableViewCell
theo chương trình. Nhưng tôi cần phải thêm hình ảnh vào từng ô. Làm thế nào tôi có thể làm điều đó?Cách thêm hình ảnh vào ô UITableView?
Đây là mã của tôi. .h nộp
@interface BidalertsViewController : UIViewController <UITableViewDelegate,UITableViewDataSource> {
NSArray *listData;
}
@property(nonatomic, retain) NSArray *listData;
@end
.m tập tin
@synthesize listData;
- (void)viewDidLoad {
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(5,0,310,28)];
newView.backgroundColor=[UIColor whiteColor];
UITextView *mytext = [[UITextView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 25.0)];
mytext.backgroundColor = [UIColor clearColor];
mytext.textColor = [UIColor blackColor];
mytext.editable = NO;
mytext.font = [UIFont systemFontOfSize:15];
mytext.text = @"Asset Name";
[mytext release];
[newView addSubview:mytext];
[self.view addSubview:newView];
[newView release];
NSArray *array = [[NSArray alloc] initWithObjects:@"iPhone", @"iPod", @"iPad",nil];
self.listData = array;
[array release];
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[listData dealloc];
[super dealloc];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] autorelease];
}
NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
UIImageView *imv = [[UIImageView alloc]initWithFrame:CGRectMake(3,2, 20, 25)];
imv.image=[UIImage imageNamed:@"user.jpg"];
[cell.contentView addSubview:imv];
[imv release];
}
@end
Mã không được thêm hình ảnh vào các tế bào. Có chuyện gì vậy? Làm cách nào để thêm hình ảnh tùy chỉnh vào ô?
được hiển thị. nhưng hình ảnh được hiển thị ở phía trước văn bản. văn bản không được hiển thị đầy đủ. –
hoạt động bình thường vì 'textLabel' có khung giao cắt với' UIImageView' của bạn. Có lẽ bạn cần cho ô tùy chỉnh. – beryllium