2011-06-16 99 views
0

單擊按鈕,我在表格視圖中加載一些圖像。圖像加載正常並位於所需的位置。但當我滾動表格視圖時,圖像似乎會發生變化。讓我詳細說明了在我的表視圖中最初只有2行可見,並假設圖像在兩行中存在,並且在一行中有4個圖像。現在,當我向下或向上滾動表視圖時,在表格上方或下方滾動的行視圖框架將在其圖像視圖中顯示新圖像。並且每次當我滾動圖像不斷變化時。可能是什麼原因。我正在用這個抓我的頭。請幫助。我發佈了我的代碼的一部分: -在滾動時顯示不同圖像的表格視圖

-(UITableViewCell*)tableView(UITableView*) 
ableViewcellForRowAtIndexPath(NSIndexPath*)indexPath { 

    UITableViewCell *cell = nil; 
    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier"; 
    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier]; 
    if (cell == nil) { 
     cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:AutoCompleteRowIdentifier] autorelease]; 

    } 
    UIImageView * imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 80, 80)] autorelease]; 
    UIImageView * imageView2 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,80, 80)] autorelease]; 
    UIImageView * imageView3 = [[[UIImageView alloc] initWithFrame:CGRectMake(205,4, 80, 80)] autorelease]; 
    UIImageView * imageView4 = [[[UIImageView alloc] initWithFrame:CGRectMake(295,4, 80, 80)] autorelease]; 
    imageView1.tag = 1; 
    imageView2.tag = 2; 
    imageView3.tag = 3; 
    imageView4.tag = 4; 
    [cell.contentView addSubview:imageView1]; 
    [cell.contentView addSubview:imageView2]; 
    [cell.contentView addSubview:imageView3]; 
    [cell.contentView addSubview:imageView4]; 

    UIImageView * imageView; 
    for (int i = 1; i <= 4; i++) { 
     imageView = (UIImageView *)[cell.contentView viewWithTag:i]; 
     imageView.image = nil; 

    } 

    int photosInRow; 
    if ((indexPath.row < [tableView numberOfRowsInSection:indexPath.section] - 1) || 
     (count % 4 == 0)) { 
     photosInRow = 4; 
    } else { 
     photosInRow = count % 4; 
    } 

    for (int i = 1; i <= photosInRow; i++) { 
     imageView = (UIImageView *)[cell.contentView viewWithTag:i]; 

     [self setImage1:imageView]; 

    } 

    return cell; 
} 


-(void)setImage1:(UIImageView *)imageView 
{ 

    UIImageView *imageView1=[[UIImageView alloc]init]; 

    imageView1=imageView; 
    imageView1.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.png", j]]; 

    j++; 

} 

任何幫助wi將不勝感激。

感謝, 克里斯蒂

+0

如何'setImage1:'執行? – 2011-06-16 04:38:37

+0

看到我編輯的問題 – Christina 2011-06-16 05:09:11

+0

編輯我的答案。 – 2011-06-16 05:17:09

回答

1

您的setImage1:方法應該修改爲拍攝照片索引,因爲j不是跟蹤當前圖像的正確方法,因爲cellForRowAtIndexPath:可能以任何順序調用。

- (void)setImage1:(UIImageView *)imageView forPhotoIndex:(NSInteger)index { 
    imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%ld.png", index]]; 
} 

cellForRowAtIndexPath:小的修改將是,

[..] 
for (int i = 1; i <= photosInRow; i++) { 
    imageView = (UIImageView *)[cell.contentView viewWithTag:i]; 
    [self setImage1:imageView forPhotoIndex:(indexPath.row * 4 + i - 1)]; 
} 
[..] 
+0

是的,我也試過,但仍然有相同的結果 – Christina 2011-06-16 04:25:10

+0

感謝很多精美的工作,現在可以告訴我選擇圖像在表格視圖中。當我點擊圖像時,整個單元格被選中 – Christina 2011-06-16 05:37:35

0

當重用一個細胞,你現在只需要添加新的UImageView實例。重用的單元格已經添加了子視圖,您只需添加更多子視圖。請務必只添加新的UIImageViews如果小區沒有再使用前(如果 - 子句中)

if (cell == nil) { 
    cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:AutoCompleteRowIdentifier] autorelease]; 

    UIImageView * imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(25, 4, 80, 80)] autorelease]; 
    UIImageView * imageView2 = [[[UIImageView alloc] initWithFrame:CGRectMake(115,4,80, 80)] autorelease]; 
    UIImageView * imageView3 = [[[UIImageView alloc] initWithFrame:CGRectMake(205,4, 80, 80)] autorelease]; 
    UIImageView * imageView4 = [[[UIImageView alloc] initWithFrame:CGRectMake(295,4, 80, 80)] autorelease]; 
    imageView1.tag = 1; 
    imageView2.tag = 2; 
    imageView3.tag = 3; 
    imageView4.tag = 4; 
    [cell.contentView addSubview:imageView1]; 
    [cell.contentView addSubview:imageView2]; 
    [cell.contentView addSubview:imageView3]; 
    [cell.contentView addSubview:imageView4]; 
} 

編輯:見設置實際圖像有關的問題下面的評論。

+0

早些時候我已經這樣做了,如果(單元格== nill)部分只,但也是同樣的結果,這就是現在改變了代碼,但它仍然不工作 – Christina 2011-06-16 04:24:24

+0

我假設你在某處跟蹤'count'variable?你怎麼做到這一點?如果你調用'[self setImage1:imageView];',那裏是用來設置正確圖像的計數值?如果是的話,請考慮一下,向上滾動時調用'(UITableViewCell *)tableView(UITableView *)ableViewcellForRowAtIndexPath(NSIndexPath *)indexPath'的順序不是_in sequence_,所以這可能是您的問題。 – marcus 2011-06-16 04:34:02

+0

是它顯示正確的圖像,,所以我必須做的,以保持順序滾動 – Christina 2011-06-16 05:10:04