Get the App
SLTechnology News&Howtos  ›  Development  › 

How to reload data and refresh cell by tableView

Shulou Source: shulou.com Published: 2022-06-02 03:46:22 10月31日 Update

This article shows you how tableView reloads data and refreshes cell. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

# import "NewTitleTableViewController.h"

@ interface NewTitleTableViewController ()

@ 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 set the request body

[request setHTTPBody:parmData]

/ / 7 set the request method

[request setHTTPMethod:@ "POST"]

Asynchronous POST

[NSURLConnection connectionWithRequest:request delegate:self]

}

Agent method:

-(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 method:

# 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 method

-(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 = @ "loading...."

}

Else

{

Cell.textLabel.text = self.arr [indexPath.row] [@ "title"]

}

Return cell

}

The above is how tableView reloads data and refreshes cell. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Tags: Methods data content skills knowledge conciseness brevity that is articles methods more articles industries information channels channels one light agents reserves Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Shulou Tech Info Shulou Technology NVidia Xiaomi