2011-06-15 86 views
0

我有一個表格視圖和其中的一些按鈕。第一排4個按鈕,第二排4個按鈕,等等。我正在使用數組向按鈕添加圖像。在重新載入表格視圖中重新加載表格視圖中的按鈕中的圖像

它第一次正常工作,但刪除數組中的所有項目和新對象到數組,然後調用重新加載表視圖時,新的數據不會重新加載。

可能是什麼原因?

我張貼我的代碼的一部分:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    // if (cell == nil) { 
    // cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease]; 
    // } 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 

    if(indexPath.row == 0) 
    { 
     button1=[[UIButton alloc] initWithFrame:CGRectMake(15,5,100,87)]; 
     [button1 setImage:[array objectAtindex:0] 
     [button1 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell.contentView addSubview:self.button1]; 

     button2=[[UIButton alloc] initWithFrame:CGRectMake(135,5,100,87)]; 
     [button2 setImage:[array objectAtindex:1] forState:UIControlStateNormal]; 
     [button2 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell.contentView addSubview:button2]; 
    } 
    else if(indexPath.row == 1) 
    { 
     button3=[[UIButton alloc] initWithFrame:CGRectMake(15,5,100,87)]; 
     [button3 setImage:[array objectAtindex:2] forState:UIControlStateNormal]; 
     [button3 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell.contentView addSubview:button3]; 


     button4=[[UIButton alloc] initWithFrame:CGRectMake(135,5,100,87)]; 
     [button4 setImage:[array objectAtindex:3] forState:UIControlStateNormal]; 
     [button4 addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside]; 
     [cell.contentView addSubview:button4]; 
    } 
    else 
    { 
     // Do something here 
    } 
    return cell; 
} 

我的點擊按鈕:

-(IBAction)clickToDisplayImage { 
    [text resignFirstResponder]; 
    if([imageArray count] >0) 
    { 
     [imageArray removeAllObjects]; 
    } 
    int totalImages = [text.text intValue]; 

    for(int i=0;i<totalImages;i++) 
    { 
     [imageArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d.png", i]]]; 
    } 
    [tableView reloadData]; 

}

+0

Hrm。不知道爲什麼人們在這裏投票。很清楚這裏的問題是什麼。 – RedBlueThing 2011-06-15 05:31:57

+0

你能解決你的問題嗎? – RedBlueThing 2011-06-16 05:18:45

+0

尚未......... – Christina 2011-06-16 05:45:20

回答

0

當您在的UITableView,你的cellForRowAtIndexPath調用reloadData消息將被調用來填充行。因爲您已經從第一遍創建了單元格,所以當您調用dequeueReusableCellWithIdentifier時,您將返回與@「單元」標識符關聯的最初創建的單元格。

您可以通過設置斷點並檢查第二次通過您將從dequeueReusableCellWithIdentifier(而不是新創建的單元格)獲取您的單元格來確認此情況。然後,您將爲該原始單元格添加新的子視圖(因此該單元格的四個按鈕子視圖,然後是六個,八個等等)。

你應該做的只是在第一次創建單元格時添加按鈕子視圖(因此將按鈕創建的東西移動到if(cell == nil)塊中),並且您應該使用基於錶行。例如:

- (UIButton*) getButtonLeft:(BOOL)left atIndex:(int)index 
{ 
    UIButton * button =[[UIButton alloc] initWithFrame:CGRectMake((left ? 15 : 135),5,100,87)]; 
    [button setImage:[array objectAtindex:index] forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(ClickTo:) forControlEvents:UIControlEventTouchUpInside]; 
    return button; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // both rows are different so we should use a different re-use id. 
    NSString * CellIdentifier= [NSString stringWithFormat:@"Cell%d", indexPath.row]; 

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

     switch (indexPath.row) 
     { 
      case 0: 
       button1 = [self getButtonLeft:YES atIndex:indexPath.row]; 
       [cell.contentView addSubview:button1]; 
       break; 
      case 1: 
       button2 = [self getButtonLeft:NO atIndex:indexPath.row]; 
       [cell.contentView addSubview:button2]; 
       break; 
      case 2: 
       button3 = [self getButtonLeft:YES atIndex:indexPath.row]; 
       [cell.contentView addSubview:button3]; 
       break; 
      case 3: 
       button4 = [self getButtonLeft:NO atIndex:indexPath.row]; 
       [cell.contentView addSubview:button4]; 
       break; 
      default: 
       // other cells? 
       break; 
     } 
    } 

    // if you need to modify the cell you have dequeued/created this is where you would do that 

    return cell; 
} 

N.B.我沒有通過編譯器運行此代碼,因此軟件語法錯誤;)

+0

對不起,因爲我沒有做過這樣的事情,所以我有點困惑,請ca你改變我的代碼,並請給我看 – Christina 2011-06-15 05:12:03

+0

@Christina當然。我會更新我的答案:) – RedBlueThing 2011-06-15 05:15:24

+0

wats這getbuttonleft的目的和我寫這個 – Christina 2011-06-15 05:45:40

相關問題