溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

tableView如何重載數據及刷新cell

發布時間:2022-01-07 21:28:38 來源:億速云 閱讀:190 作者:柒染 欄目:開發技術

本篇文章為大家展示了tableView如何重載數據及刷新cell,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。

#import "NewTitleTableViewController.h"

@interface NewTitleTableViewController ()<NSURLConnectionDataDelegate>
@property (nonatomic,retain) NSArray * arr;
@property (nonatomic ,retain) NSMutableData * data ;
@end

@implementation NewTitleTableViewController
- (void)dealloc
{
    self.data = nil;
    self.arr = nil;
    [super dealloc];
}
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
       
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSString * urlString = @"http://XXXXX";
    NSURL * url = [NSURL URLWithString:urlString];
    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
    NSString * parm = [NSString stringWithFormat:@"%@",@"?date=20131129&startRecord=10&len=30&udid=1234567890&terminalType=Iphone&cid=213"];
    NSData * parmData = [parm dataUsingEncoding:NSUTF8StringEncoding];
    //6 設置請求體
    [request setHTTPBody:parmData];
    //7 設置請求方式
    [request setHTTPMethod:@"POST"];
   

異步POST
    [NSURLConnection connectionWithRequest:request delegate:self];
  
}

代理方法:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    self.data = [NSMutableData data];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.data appendData:data];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    
    NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:self.data options:NSJSONReadingMutableContainers error:nil];
        self.arr = dic[@"news"];
    [self.tableView reloadData];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    
}


tableView datadelegate  方法:

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (self.arr.count == 0) {
        return 1;
    }
    return self.arr.count;
}

cell 方法

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * iden = @"fff";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (!cell) {
        cell = [[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden] autorelease];
    }
    
    if (self.arr[indexPath.row][@"title"] == nil)
    {
        cell.textLabel.text = @"加載中....";
    }
    else
    {
    cell.textLabel.text = self.arr[indexPath.row][@"title"];
    }
    return cell;
}

上述內容就是tableView如何重載數據及刷新cell,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女