iphone - 如果第一次填充,如何限制 tableview 第二次填充单元格

标签 iphone ios objective-c uitableview

在我的 TableView 中,我设置了图像和两个标签,它填充得很好。但是当我向下滚动 TableView 然后重新查看填充的单元格时,它会重新填充。现在我用来自 URL 的图像填充我的 ImageView,这样它会为我的用户创建负载。

那么我是否可以限制我的 TableView,如果我的单元格第一次被填充,它就不会再次填充。

这是我的代码:

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

    static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

    UITableViewCell * cell = [atableView  dequeueReusableCellWithIdentifier: nil];

    if(cell == nil) {

        cell = [[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:SimpleTableIdentifier];

        ChannelList *channelList = nil;
        if (searchedData.count ==0) {
            channelList = [channelAllData objectAtIndex:[indexPath section]];
        } else {
            channelList = [searchedData objectAtIndex:[indexPath section]];
        }


        UIImageView *aImageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 10, 50, 50)];
        aImageView.image = [UIImage imageNamed:@"tv-defolt.png"];
        NSString * mystr = [channelList channelLogo];

        if (mystr.length !=0) {
            //get a dispatch queue
            dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            //this will start the image loading in bg
            dispatch_async(concurrentQueue, ^{
                NSString * str = [NSString stringWithFormat:@"http://manektech.net/mttv/Channel/%@",[channelList channelLogo]];
                NSData *image = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:str]];

                //this will set the image when loading is finished
                dispatch_async(dispatch_get_main_queue(), ^{
                    aImageView.image = [UIImage imageWithData:image];
                });
            });
        } else {
            aImageView.image = [UIImage imageNamed:@"tv-defolt.png"];
        }

        [cell addSubview:aImageView];


        UILabel *nameTextLabel =  [[UILabel alloc] initWithFrame: CGRectMake(80, 15, 175, 20)];
        nameTextLabel.textColor = [UIColor blackColor];
        nameTextLabel.backgroundColor = [UIColor clearColor];
        nameTextLabel.text = [channelList channelName];
        nameTextLabel.font = [UIFont fontWithName:@"Arial" size:14];
        [cell addSubview:nameTextLabel];

        UILabel *genreTextLabel =  [[UILabel alloc] initWithFrame: CGRectMake(80, 35, 175, 20)];
        genreTextLabel.textColor = [UIColor grayColor];
        genreTextLabel.backgroundColor = [UIColor clearColor];

        NSString * str = [NSString stringWithFormat:@"%@ | %@",[channelList channelType],[channelList channelCounry]];

        genreTextLabel.text = str;
        genreTextLabel.font = [UIFont fontWithName:@"Arial" size:14];
        [cell addSubview:genreTextLabel];


    cell.selectionStyle = UITableViewCellSelectionStyleGray;


    }
    return cell;

}

最佳答案

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

     NSString *SimpleTableIdentifier = [NSString stringWithFormat:@"Cell%d%d",indexPath.section,indexPath.row]; /// set different idenfifier for all cell

    UITableViewCell * cell = [atableView  dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:SimpleTableIdentifier] autorelease];
        /// Add UIImageView and UILable here 
    }    
}

关于iphone - 如果第一次填充,如何限制 tableview 第二次填充单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15564608/

相关文章:

objective-c - RestKit SerializationMimeType - 它是否使用相同的类型进行反序列化?

ios - 如何从包含我的 UIViewController 的 UITableViewController 中显示新 View ?

ios - 如何在中间停止 AVSpeechSynthesizer 播放?

ios - Jenkins : Invalid gitTool selection Git

iphone - 如何在我们当前的应用程序中显示从另一个应用程序传递的参数值

ios - UIView 图像中的缩放效果

ios - 在不同 Storyboard之间快速传递消息和/或对象

iphone - iOS 5 中 UINavigationBar 中的截止标题标签

android - 瞬间向10米内的一组手机发送信号

iphone - 如何检查 sqlite 文件中存在多少表(Objective-C)