2011-06-24 56 views
0

在我的應用程序中,我有一個表視圖和4圖像視圖在一行。當我滾動表視圖時,索引路徑方法的行再次被調用,然後應用程序崩潰。如何防止滾動時重新加載表格視圖。我的部分代碼是: -表視圖方法單元格索引路徑行再次重新加載,然後應用程序崩潰

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = nil; 
    static NSString *AutoCompleteRowIdentifier = @"AutoCompleteRowIdentifier"; 


    cell = [tableView dequeueReusableCellWithIdentifier:AutoCompleteRowIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:AutoCompleteRowIdentifier] autorelease]; 

     for (int i=0; i <= [wordsInSentence count]; ++i) { 
      UIImageView *imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(30+90*(i%4), 15, 80, 100)] autorelease] ; 
      imageView1.tag = i+1; 

      [imageViewArray insertObject:imageView1 atIndex:i]; 
      [cell.contentView addSubview:imageView1]; 
     } 

    } 

    int photosInRow; 

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

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

    return cell; 
} 

請幫忙。任何幫助將不勝感激。

感謝, 克里斯蒂

+0

@Christian請花一些時間和格式化您的問題。它有助於。 –

+0

sry會在將來發出砰砰聲,請給我答案 – Christina

回答

1

唯一的問題我看是這樣的,

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

什麼是j在這裏?我建議你改變,要i

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

因此唯一的其他缺陷我看到的是這裏的邏輯,

for (int i=0; i <= [wordsInSentence count]; ++i) { 
    UIImageView *imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(30+90*(i%4), 15, 80, 100)] autorelease] ; 
    imageView1.tag = i+1; 

    [imageViewArray insertObject:imageView1 atIndex:i]; 
    [cell.contentView addSubview:imageView1]; 
} 

你只需要連續加4個圖像視圖。不是每行中圖像視圖的總數。這是有缺陷的邏輯。建議更新到

for (int i = 0; i < 4; ++i) { 
    UIImageView *imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(30+90*(i%4), 15, 80, 100)] autorelease] ; 
    imageView1.tag = i+1; 

    int trueImageIndex = indexPath.row * 4 + i; 
    [imageViewArray insertObject:imageView1 atIndex:trueImageIndex]; 

    [cell.contentView addSubview:imageView1]; 
} 
相關問題