2011-12-15 13 views
2

的麻煩我有一個UITableView其中每個UITableViewCell具有對他們UIView和以上UIView我有兩個UIButton和兩個UILabel。我的問題是,當我重新加載tableview,然後新的數據重寫舊數據,使內容模糊和不清楚,並在方法cellForRowAtIndexPath我使用此代碼刪除標籤,然後它也不能正常工作...... 。的UITableView reloadData方法創建模糊數據

NSArray *subviews = [[NSArray alloc] initWithArray:cell.contentView.subviews]; 
    for (UILabel *subview in subviews) { 
     [subview removeFromSuperview]; 
    } 
    [subviews release]; 

此代碼是一次顯示否則沒有人行...

cellForRowAtIndexPath的代碼是這樣的....

- (UITableViewCell *)tableView:(UITableView *)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    static NSString *identifier = @"Cell"; 
    UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:identifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; 
    } 

    cell.selectionStyle = UITableViewCellSelectionStyleNone; 
    NSDictionary *dict = [appDelegate.webDataList objectAtIndex:indexPath.row]; 

    tableView.backgroundColor = [UIColor colorWithRed:0.39 green:0.39 blue:0.39 alpha:0.39]; 

    if([appDelegate.dataArray count]>0){ 

     imgView.backgroundColor = [UIColor clearColor]; 
     imgView =[[UIView alloc ] initWithFrame:CGRectMake(0,0,320,45)]; 
     imgView.tag= indexPath.row+250; 
     [cell.contentView addSubview:imgView]; 

     button=[UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame=CGRectMake(291, 5, 19, 21); 
     img =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"swap_re" ofType:@"png"]]; 
     [button setBackgroundImage:img forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(swapButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
     button.tag= indexPath.row+250; 
     [imgView addSubview:button]; 

     button=[UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame=CGRectMake(-25, 5, 19, 21); 
     img =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"swap_re" ofType:@"png"]]; 
     [button setBackgroundImage:img forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(swapButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
     button.tag= indexPath.row+250; 
     [imgView addSubview:button]; 

     button=[UIButton buttonWithType:UIButtonTypeCustom]; 
     button.frame=CGRectMake(5, 7, 19, 21); 
     button.tag = indexPath.row+250; 
     img =[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"delete" ofType:@"png"]]; 
     [button setBackgroundImage:img forState:UIControlStateNormal]; 
     [button addTarget:self action:@selector(deleteButtonPressed:) forControlEvents:UIControlEventTouchUpInside];  
     [imgView addSubview:button]; 

     NSArray *arr = [[appDelegate.dataArray objectAtIndex:indexPath.row] componentsSeparatedByString:@"/"]; 


     UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(34,9,140,20)]; 
     label.text = [arr objectAtIndex:1]; 
     label.tag = indexPath.row+250; 
     [label setFont:[UIFont fontWithName:@"Georgia" size:14]]; 
     [label setNumberOfLines:0]; 
     label.textAlignment = UITextAlignmentLeft; 
     label.textColor = [UIColor whiteColor]; 
     label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; 
     label.backgroundColor = [UIColor clearColor]; 
     [imgView addSubview:label]; 
     [label release]; 

     NSDictionary *dict1 =[appDelegate.webDataList objectAtIndex:indexPath.row]; 


     label = [[UILabel alloc] initWithFrame:CGRectMake(181, 7, 75, 20)]; 
     label.tag = indexPath.row+250; 
     label.text = [dict1 objectForKey:@"time"]; 
     [label setFont:[UIFont fontWithName:@"Georgia" size:16.0]]; 
     label.textAlignment = UITextAlignmentLeft; 
     [label setBackgroundColor:[UIColor clearColor]]; 
     label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; 
     [label setTextColor:[UIColor whiteColor]]; 
     [imgView addSubview:label]; 
     [label release]; 
    } 
    return cell; 
} 

,並在我的代碼我是因爲不能使用方法reuseTableViewCellWithIdentifier:我希望每個標籤和按鈕標籤都應該相同,因爲我必須在其他位置獲取它們。

回答

3

發生這種情況是因爲你做錯了。你甚至不需要重新加載表格。只要滾動,你會發現它。
每次顯示單元格時都會創建新標籤和按鈕。而這些UI元素當然不會被刪除。

當您創建新單元格併爲其添加標籤時添加UI元素。 如果單元格已成功出列,請通過該標籤獲取元素並設置其值。

像這樣:

if (cell == nil) { 
    // if no cell could be dequeued create a new one 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease]; 

    // add label to the new cell 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(34,9,140,20)]; 
    label.tag = 250; // <---- save tag 
    [label setFont:[UIFont fontWithName:@"Georgia" size:14]]; 
    [label setNumberOfLines:0]; 
    label.textAlignment = UITextAlignmentLeft; 
    label.textColor = [UIColor whiteColor]; 
    label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight; 
    label.backgroundColor = [UIColor clearColor]; 
    //[imgView addSubview:label]; // I ignore this because I don't want to write 200 lines of code here. This is just an example so please adopt the code yourself 
    [cell.contentView addSubView:label]; 
    [label release]; 
} 

// whatever happened you have a cell with all the labels added here 

UILabel *label = (UILabel *)[cell.contentView viewWithTag:250]  // <---- get label with tag 
label.text = [arr objectAtIndex:1]; 

,你不需要if([appDelegate.dataArray count]>0)。如果您的所有其他tableview數據源方法都正確tableView:cellForRowAtIndexPath:如果您的數據源爲空,則不會被調用。


編輯:剛纔看到一個:

,並在我的代碼我不能夠使用的方法reuseTableViewCellWithIdentifier:因爲我想每個標籤和按鈕標籤應該是相同的,因爲我要取他們在哪裏。

其實你可以。大家可以也應該使用reuseTableViewCellWithIdentifier:
你有什麼確切的問題呢?
如果你需要在按鈕操作方法中獲得indexPath(或者在你的情況下只有一行),請使用我的代碼answer to another question

+0

好的,我會嘗試進行更改 – shivangi 2011-12-15 13:01:19