---------------------------------------------------------------------------------------MyCell.h---------------------------------------------------------------------------------------
#import <UIKit/UIKit.h>
@interface MJCell : UITableViewCell
@property (retain, nonatomic) IBOutlet UILabel *firstLabel;
@property (retain, nonatomic) IBOutlet UILabel *midLabel;
@property (retain, nonatomic) IBOutlet UILabel *rightLabel;
- (void)setValue:(NSDictionary *)dic;
@end
---------------------------------------------------------------------------------------MyCell.m---------------------------------------------------------------------------------------
#import "MJCell.h"
@implementation MJCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selectedanimated:animated];
// Configure the view for the selected state
}
- (void)setValue:(NSDictionary *)dic{
_firstLabel.text=[dicobjectForKey:@"mc"];
_midLabel.text=[dicobjectForKey:@"jz"];
_rightLabel.text=[dicobjectForKey:@"ncount"];
}
- (void)dealloc {
[_firstLabel release];
[_midLabel release];
[_rightLabel release];
[super dealloc];
}
@end
--------------------------------------------------------------------------------------- jrkcViewController.h---------------------------------------------------------------------------------------
#import <UIKit/UIKit.h>
@interface jrkcViewController :UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)NSArray *listData;
@end
---------------------------------------------------------------------------------------jrkcViewController.m---------------------------------------------------------------------------------------
#import "jrkcViewController.h"
#import "ServiceHelper.h"
#import "SoapXmlParseHelper.h"
//#import "MJCell.h"
@interface jrkcViewController ()
@end
@implementation jrkcViewController
@synthesize tableView=_tableView;
@synthesize listData=_listData;
//@synthesize customCell;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
//获取今天的日期
-(NSString *)getDate{
NSDateFormatter*formatter = [[NSDateFormatteralloc]init];
[formatter setDateFormat:@"yyyyMMdd"];
NSString *locationString=[formatter stringFromDate: [NSDate date]];
[formatter release];
return locationString;
}
- (void)viewDidLoad
{
[superviewDidLoad];
self.view.backgroundColor=[UIColorwhiteColor];
NSString *now=[selfgetDate];
UIBarButtonItem *leftbtn=[[UIBarButtonItemalloc]initWithTitle:@"back"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(buttonBack)];
self.navigationItem.leftBarButtonItem=leftbtn;
[leftbtn release];
NSUserDefaults *user=[NSUserDefaultsstandardUserDefaults];
NSString *name=[user objectForKey:@"UserName"];
self.tableView=[[UITableViewalloc] initWithFrame:CGRectMake(0,30,self.view.bounds.size.width,self.view.bounds.size.height-30)];
self.tableView.dataSource=self;
self.tableView.delegate=self;
[self.view addSubview:self.tableView];
//传递参数
NSMutableArray *params=[NSMutableArrayarray];
[params addObject:[NSDictionarydictionaryWithObjectsAndKeys:now,@"start_date",nil]];
[params addObject:[NSDictionarydictionaryWithObjectsAndKeys:now,@"end_date",nil]];
[params addObject:[NSDictionarydictionaryWithObjectsAndKeys:name,@"ygdm",nil]];
[params addObject:[NSDictionarydictionaryWithObjectsAndKeys:@"1",@"Flag",nil]];
//设置传递对象
ServiceArgs *args=[[[ServiceArgsalloc]init]autorelease];
args.methodName=@"SaleKClfxNEW";
args.soapParams=params;
//调用
self.listData=[NSArrayarray];
[[ServiceHelper sharedInstance]asynService:argscompleted:^(NSString *xml,NSDictionary *userInfo) {
//NSLog(@"xml=%@\n",xml);
NSArray *arr=[SoapXmlParseHelpersearchNodeToArray:xmlnodeName:@"Table"];
self.listData=arr;
[self.tableViewreloadData];
NSLog(@"result===%@\n",arr);
} failed:^(NSError *error,NSDictionary *userInfo) {
NSLog(@"error=%@\n",[errordescription]);
}];
// Do any additional setup after loading the view from its nib.
}
- (void)buttonBack{
[selfdismissViewControllerAnimated:YEScompletion:nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [self.listDatacount];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier=@"MJCell";
//MJCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell==nil) {//UITableViewCellStyleValue1
/***
NSArray *arr=[[NSBundle mainBundle] loadNibNamed:@"MJCell" owner:self options:nil];
if ([arr count]>0) {
cell=[arr objectAtIndex:0];
NSLog(@"aa\n");
}else{
// cell=[[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:cellIdentifier] autorelease];
}
**/
cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentifier]autorelease];
UILabel *label1=[[[UILabelalloc]initWithFrame:CGRectMake(0,0,100,44)]autorelease];
label1.tag=100;
label1.font=[UIFontsystemFontOfSize:8];
UILabel *label2=[[[UILabelalloc]initWithFrame:CGRectMake(100,0,100,44)]autorelease];
label2.tag=101;
UILabel *label3=[[[UILabelalloc]initWithFrame:CGRectMake(200,0,100,44)]autorelease];
label3.tag=102;
[cell.contentView addSubview:label1];
[cell.contentView addSubview:label2];
[cell.contentView addSubview:label3];
}
NSDictionary *dic=[self.listDataobjectAtIndex:indexPath.row];
UILabel *label1=(UILabel*)[cellviewWithTag:100];
UILabel *label2=(UILabel*)[cellviewWithTag:101];
UILabel *label3=(UILabel*)[cellviewWithTag:102];
label1.text=[dic objectForKey:@"mc"];
label2.text=[dic objectForKey:@"jz"];
label3.text=[dic objectForKey:@"ncount"];
// cell.detailTextLabel.text=[dic objectForKey:@"jz"];
// cell.detailTextLabel.text=[dic obje=ctForKey:@"ncount"];
return cell;
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)dealloc{
[super dealloc];
[_tableView release];
[_listData release];
// [customCell release];
}
@end
觉得写得不错,帮忙打赏下,谢谢!
iOS表格视图数据加载
本文介绍了一个iOS应用中使用UITableView展示数据的具体实现方法。通过自定义UITableViewCell来显示从服务器获取的数据,并详细展示了如何配置单元格、加载数据及解析响应。

被折叠的 条评论
为什么被折叠?



