2010-07-15 66 views
0

我正在表的每一行中添加一個活動指示器。這個問題是每次我滾動它重新添加在單元覆蓋前一個。請讓我知道哪些是在tableview單元格中添加控件的最佳方式。UITableView在iphone中添加控制問題

UIActivityIndicatorView *a; 

//自定義表格視圖單元格的外觀。

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

    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    } 
    a = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
    [a startAnimating]; 
    [cell.contentView addSubview:a]; 

    // Configure the cell. 

    return cell; 
} 

回答

0

添加指示劑細胞只有當你創建它,那麼你可以使用它的標籤屬性來訪問它:

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
if (cell == nil) { 
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 
    a = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
    a.tag = 10; 
    [cell.contentView addSubview:a]; 
} 
UIActivityIndicatorView* aView = [cell.contentView viewWithTag:10]; 
[aView startAnimating]; 
+0

非常感謝的正是這種「[cell.contentView viewWithTag:10]」我想知道。再次感謝 – iPhoneDev 2010-07-15 12:20:26