【问题标题】:UITableView add control issue in iphoneiphone中的UITableView添加控件问题
【发布时间】:2010-07-15 20:08:06
【问题描述】:

我在表格的每一行中添加一个活动指示器。问题是每次我滚动它时,它都会再次添加到覆盖前一个的单元格中。请让我知道在表格视图单元格中添加控件的最佳方法。

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;
}

【问题讨论】:

    标签: iphone uitableview


    【解决方案1】:

    只有在创建单元格时才向单元格添加指示符,然后您可以使用其标记属性访问它:

    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];
    

    【讨论】:

    • 非常感谢我想知道的这个“[cell.contentView viewWithTag:10]”。再次感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    相关资源
    最近更新 更多