更新tableView的某个cell

更新tableView的某个cell

异步加载完数据后更新某个cell,这应该是非常常见的使用方法了,我们经常会用reloadData.

效果:

源码:

//
//  RootViewController.m
//  DataTableView
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "SDWebImage.h"

@interface RootViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView  *showTableView;
@property (nonatomic, strong) NSArray      *dataArray;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 初始化tableView
    _showTableView = [[UITableView alloc] initWithFrame:self.view.bounds
                                                  style:UITableViewStylePlain];
    _showTableView.delegate   = self;
    _showTableView.dataSource = self;
    [self.view addSubview:_showTableView];

    // 下载数据源
    NSString *oneImageURL = @"http://pic.cnitblog.com/avatar/607542/20140226182241.png";
    [[SDWebImageManager sharedManager]
     downloadWithURL:[NSURL URLWithString:oneImageURL]
     options:0
     progress:^(NSInteger receivedSize, NSInteger expectedSize)
     {

     }
     completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
     {
         _dataArray = @[image];

         // 更新某个cell的数据
         [_showTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]]
                               withRowAnimation:UITableViewRowAnimationFade];
     }];
}

#pragma mark - UITableView delegate & dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reusedID = @"YouXianMing";

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

    // 第一个cell
    if (indexPath.row == 0)
    {
        if ([_dataArray count] > 0)
        {
            cell.imageView.image = _dataArray[0];
        }
    }

    // 第二个cell
    if (indexPath.row == 1)
    {
        if ([_dataArray count] > 0)
        {
            cell.imageView.image = _dataArray[0];
        }
    }

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

@end

核心:

最主要的是,reloadRowsAtIndexPaths能有动画效果.

 

附录:

重新给了高度值也是有动画的哦:)

源码:

//
//  RootViewController.m
//  DataTableView
//
//  Copyright (c) 2014年 Y.X. All rights reserved.
//

#import "RootViewController.h"
#import "SDWebImage.h"

@interface RootViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView  *showTableView;
@property (nonatomic, strong) NSArray      *dataArray;

@end

@implementation RootViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // 初始化tableView
    _showTableView = [[UITableView alloc] initWithFrame:self.view.bounds
                                                  style:UITableViewStylePlain];
    _showTableView.delegate   = self;
    _showTableView.dataSource = self;
    [self.view addSubview:_showTableView];

    // 下载数据源
    NSString *oneImageURL = @"http://wallpapers.wallbase.cc/rozne/wallpaper-573934.jpg";
    [[SDWebImageManager sharedManager]
     downloadWithURL:[NSURL URLWithString:oneImageURL]
     options:0
     progress:^(NSInteger receivedSize, NSInteger expectedSize)
     {
         NSLog(@"%f", (float)receivedSize/(float)expectedSize);
     }
     completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
     {
         _dataArray = @[image];

         // 更新某个cell的数据
         [_showTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]]
                               withRowAnimation:UITableViewRowAnimationFade];
     }];
}

#pragma mark - UITableView delegate & dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *reusedID = @"YouXianMing";

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

    // 第一个cell
    if (indexPath.row == 0)
    {
        if ([_dataArray count] > 0)
        {
            cell.imageView.image = _dataArray[0];
        }
    }

    // 第二个cell
    if (indexPath.row == 1)
    {
        if ([_dataArray count] > 0)
        {
            cell.imageView.image = _dataArray[0];
        }
    }

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 加载完数据后动态展开
    if (indexPath.row == 0)
    {
        if ([_dataArray count] > 0)
        {
            return 200;
        }
    }

    return 70;
}

@end

核心:

时间: 2024-07-28 19:53:43

更新tableView的某个cell的相关文章

使用HVTableView动态展开tableView中的cell

使用HVTableView动态展开tableView中的cell 效果: 源码: HVTableView.h 与 HVTableView.m // // HVTableView.h // HRVTableView // // Created by Hamidreza Vakilian on 25/11/2013 // Copyright (c) 2013 Hamidreza Vakilian. All rights reserved. // Website: http://www.infracy

优化tableView加载cell与model的过程

优化tableView加载cell与model的过程   效果图   说明 1. 用多态的特性来优化tableView加载cell与model的过程 2. swift写起来果然要比Objective-C简洁了不少   源码 https://github.com/YouXianMing/Swift-TableViewDemo https://github.com/YouXianMing/OC-TableViewDemo // // ViewController.swift // Swift-Tab

动态切换tableView中的cell的种类

动态切换tableView中的cell的种类 为什么要动态切换tableView中cell的种类呢?如果项目经理不出这种需求,你也就见不到这篇文章了:) 效果: 源码: 首先,你要准备3种cell,直接继承系统的就行了. // // RootViewController.m // ChangeCell // // Copyright (c) 2014年 Y.X. All rights reserved. // #import "RootViewController.h" #import

iOS 在tableview的每个cell上添加button,button的颜色问题

问题描述 iOS 在tableview的每个cell上添加button,button的颜色问题 iOS 在tableview的每个cell上添加button,当选中时候,button的颜色为蓝色,边框宽度为1,为选中时边框颜色为灰色,边框宽度为0.5,问题是:点击其他cell中的button时候刷新单元格,button的边框颜色刷新失败,还是蓝色,但是边框宽度却刷新成功,变为0.5,如下图所示,求解刷新边框颜色失败的原因及解决方案!非常感谢!如能解决问题定及时采纳!谢谢 解决方案 你有么有把之前

ios开发-如何给一个在tableview外的button添加点击tableview内一个cell的事件?

问题描述 如何给一个在tableview外的button添加点击tableview内一个cell的事件? 小弟萌新 请教各位 我想实现一个像CSDN手机APP中的一个效果. CSDN左边栏中点击用户头像然后变成点击tableviewcell最后一行的效果. 解决方案 已经解决了,selectRowAtindexpath方法,和didselectrow方法一起使用.select方法只是选择默认cell,不能点击进去 解决方案二: 点击一下头像的按钮,给他一个点击事件 [self.tableView

ios-跪求这个数字怎么显示,在tableview中的cell,我用的vlue1,label是我自己写的。

问题描述 跪求这个数字怎么显示,在tableview中的cell,我用的vlue1,label是我自己写的. NSArray *array = [[NSArray alloc] initWithObjects:@"待审核商品", @"以审核商品", @"未通过审核", @"折扣商品", @"断货商品", nil]; self.list = array; 解决方案 可以在后面自己加一个label 解决方案二:

在uiview 的tableView中点击cell进入跳转到另一个界面的实现方法_IOS

1.先重写uiviewcontrol的方法 - (UIViewController *)viewController { for (UIView* next = [self superview]; next; next = next.superview) { UIResponder *nextResponder = [next nextResponder]; if ([nextResponder isKindOfClass:[UIViewController class]]) { return

动态展开tableView的cell[1]

动态展开tableView的cell[1] 源码地址:https://github.com/xerxes235/HVTableView 虽然作者写的demo很好看,可是,你很难理解他是怎么玩的-_-!!,不信,你可以去下载他的demo试一下:) 本人运行时的效果如下图: 源码: RootViewController.m // // RootViewController.m // AnimationTableView // // Copyright (c) 2014年 Y.X. All right

用TableView写带特效的cell

用TableView写带特效的cell 效果: 源码地址: https://github.com/YouXianMing/UI-Component-Collection 分析: 在UIScrollView中的代理中发送广播,然后在cell中接收广播 对每一个cell进行设置 对开发有利的一种小细节:   核心源码: 控制器源码 // // ViewController.m // TableView // // Created by XianMingYou on 15/4/9. // Copyri