我已經開始使用IOS開發,並且在我的第一個客戶端項目上。動態添加多個圖像到TableView
我堅持在下列地點:
所以如果有這個月是10人的生日,我需要顯示在一個單行10個imageviews(3張照片,3張圖片和1張圖片)。
有人可以幫我解決上述問題。這將不勝感激。
我已經開始使用IOS開發,並且在我的第一個客戶端項目上。動態添加多個圖像到TableView
我堅持在下列地點:
所以如果有這個月是10人的生日,我需要顯示在一個單行10個imageviews(3張照片,3張圖片和1張圖片)。
有人可以幫我解決上述問題。這將不勝感激。
如果你想在tableview中添加圖像/標籤/按鈕,那麼最簡單的方法是子類UITableViewCell.For例如,如果你有5個圖像(不明白3張圖片,3張圖片和1張圖片),你想向他們展示在錶行,那麼你可以做這樣的事情:
在您的項目名爲CustomCell添加一個新的文件,它應該是的UITableViewCell像這樣子類:
@interface CustomCell : UITableViewCell {
UIImageView *imageView1;
UIImageView *imageView2;
UIImageView *imageView3;
UIImageView *imageView4;
UIImageView *imageView5;
}
@property (nonatomic, retain) UIImageView *imageView1;
@property (nonatomic, retain) UIImageView *imageView2;
@property (nonatomic, retain) UIImageView *imageView3;
@property (nonatomic, retain) UIImageView *imageView4;
@property (nonatomic, retain) UIImageView *imageView5;
@end
而且在CustomCell.m中輸入如下代碼:
@synthesize imageView1, imageView2, imageView3, imageView4, imageView5;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
{
self.imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 8, 50, 45)];
[self.contentView addSubview:imageView1];
self.imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(65, 8, 50, 45)];
[self.contentView addSubview:imageView2];
self.imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(120, 8, 50, 45)];
[self.contentView addSubview:imageView3];
self.imageView4 = [[UIImageView alloc] initWithFrame:CGRectMake(170, 8, 50, 45)];
[self.contentView addSubview:imageView4];
self.imageView5 = [[UIImageView alloc] initWithFrame:CGRectMake(225, 8, 50, 45)];
[self.contentView addSubview:imageView5];
}
return self;
}
所以,你與UITableViewCell.Now的子類做了如何在您的視圖控制器使用它們?
你必須導入CustomCell.h在視圖控制器class.And在的cellForRowAtIndexPath委託方法做一些事情是這樣的:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
//our custom cell
CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[CustomCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell.
//cell.imageView1.image = [UIImage imageNamed:@"testImg1"];
//cell.imageView2.image = [UIImage imageNamed:@"testImg2"];
//cell.imageView3.image = [UIImage imageNamed:@"testImg3"];
//cell.imageView4.image = [UIImage imageNamed:@"testImg4"];
//cell.imageView5.image = [UIImage imageNamed:@"testImg5"];
//return cell;
ü可以用更好的是:
NSMutableArray *imgArray = [[NSMutableArray alloc] initWithObjects:cell.imageView1, cell.imageView2,cell.imageView3, cell.imageView4, cell.imageView5, nil];
for (int imageCounter = 0; imageCounter<5; imageCounter++) {
[[imgArray objectAtIndex:imageCounter] setImage:[UIImage imageNamed:[NSString stringWithFormat:@"testImg%d", imageCounter]]];
}
return cell;
}
這是針對問題背景的非常原型的解決方案。
希望它能幫助你:)
- (UITableViewCell *)tableView:(UITableView *)tTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tTableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryNone;
for (int t =0; t<cell.subviews.count; t=t) {
if (![[[cell.subviews objectAtIndex:t].class]isEqualToString:@"UIImageView"]) {
t++;
}
else {
[((UIView*)[[cell.subviews objectAtIndex:t])removeFromSuperview];
}
}
}
for (int t = 0; t<10; t++) {
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(cell.frame.size.width/10*t, 0, cell.frame.size.width/10, cell.frame.size.height)];
[imageView setImage:[UIImage imageNamed:<#(NSString *)#>]];
[cell addSubview:imageView];
}
return cell;
}
我不不知道你是什麼意思(3張照片,3張圖片和1張圖片),但你應該能夠從那裏弄明白。
看起來你似乎暗示每次表格視圖要求排成一行......每次......即使只是一排滾動然後重新開始,你他建議他分配10個UIImage視圖並將它們作爲子視圖進行堆疊。這不是對這個模糊問題的過度苛刻的懲罰嗎? – danh
你也想刪除舊的或重新使用它們。 另一種方法是創建一個具有10個圖像視圖的UITableViewCell的子類,您可以設置,但是然後您需要最多可以放置在每個單元中的10張圖片。 –
如果你想要一個未定義數量的圖片,請嘗試我給你的並修改它以重用對象而不是垃圾並重新制作它們。 如果不使用http://stackoverflow.com/a/10399483/1354623 –
只需將鼠標懸停在downvote箭頭上,看看downvoting的原因,然後再次閱讀你的問題。這裏的人會幫助或指導你,而不是爲你工作。 – psycho
@Vishal Aggarwal: - 詳細描述你的要求..你想在表格視圖單元格中顯示圖像嗎?或在一個視圖? 如果你想在一個表格視圖單元中顯示10個圖像,那麼你必須實現兩種不同的方法。 1-自定義你的表格單元格或2.-創建'脈衝'風格滾動 - 水平滾動UITableView作爲UITableViewCell的子視圖。 首先告訴我你的要求詳細後,我會盡力幫助你。我希望我們都能取得成功:-) – 2012-05-01 14:10:09
@psycho - 如果缺乏信息讓你感到不快,我很抱歉。我完成了剛完成訓練的初學者。就我所知,我一定會變得更好。 :) –