动态填充 UITableView
Posted
技术标签:
【中文标题】动态填充 UITableView【英文标题】:Populating UITableView dynamically 【发布时间】:2011-12-12 05:23:23 【问题描述】:我有以下代码从 URL 中获取数据,将其存储在数组中,并将其显示在表格中。
现在的问题是我的 URL,它根据按钮单击向表格提供数据变化,即如果用户单击按钮 [gainer],数据将从与 gainer 相关的 URL 中获取等等。
由于我在 didLoad 方法中调用了 gainer 方法,最初数据是从相应的 URL 显示的。但是在按钮单击时没有采取任何操作。原因是我在方法中填充数组(增益和松散),但表在创建表时从数组中获取数据(此时数组为空)。
我在以编程方式创建表格时在设计视图中创建了按钮。
fetchdataViewController.h
#import <UIKit/UIKit.h>
@interface fetchdataViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
IBOutlet UIWebView *webView;
NSMutableArray *arr1;
NSMutableArray *atarr;
NSMutableArray *arr2;
NSMutableArray *a;
NSMutableArray *b;
NSMutableArray *c;
NSMutableArray *d;
UITableView *aTableView;
-(IBAction)btnClicked:(id)sender;
-(IBAction)btnClicked1:(id)sender;
-(void)gainer;
-(void)looser;
@property(nonatomic,retain)NSMutableArray *arr1;
@property(nonatomic,retain)NSMutableArray *arr2;
@property(nonatomic,retain)NSMutableArray *atarr;
@property(nonatomic,retain)NSMutableArray *a;
@property(nonatomic,retain)NSMutableArray *b;
@property(nonatomic,retain)NSMutableArray *c;
@property(nonatomic,retain)NSMutableArray *d;
@property(nonatomic,retain)UIWebView *webView;
@property(nonatomic,retain)UITableView *aTableView;
@end
fetchdataViewController.m
#import "fetchdataViewController.h"
@implementation fetchdataViewController
NSMutableArray *atarr;
NSMutableArray *arr1;
NSMutableArray *arr2;
NSMutableArray *a;
NSMutableArray *b;
NSMutableArray *c;
NSMutableArray *d;
NSMutableString *mainstr;
NSMutableString *str;
@synthesize webView;
@synthesize arr1;
@synthesize arr2;
@synthesize atarr;
@synthesize a;
@synthesize b;
@synthesize c;
@synthesize d;
@synthesize aTableView;
int i,j;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return [a count];
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
static NSString *CellIdentifier=@"Cell";
static NSInteger StateTag = 1;
static NSInteger CapitalTag = 2;
static NSInteger StateTag1 = 3;
static NSInteger StateTag2 = 4;
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
cell=[[[UITableViewCell alloc]initWithFrame:CGRectZero reuseIdentifier:CellIdentifier]autorelease];
CGRect frame;
frame.origin.x = 10;
frame.origin.y = 5;
frame.size.height = 35;
frame.size.width = 170;
UILabel *capitalLabel = [[UILabel alloc] initWithFrame:frame];
capitalLabel.tag = CapitalTag;
[cell.contentView addSubview:capitalLabel];
frame.origin.x += 125;
UILabel *stateLabel = [[UILabel alloc] initWithFrame:frame];
stateLabel.tag = StateTag;
[cell.contentView addSubview:stateLabel];
frame.origin.x += 100;
UILabel *stateLabel1 = [[UILabel alloc] initWithFrame:frame];
stateLabel1.tag = StateTag1;
[cell.contentView addSubview:stateLabel1];
frame.origin.x += 100;
UILabel *stateLabel2 = [[UILabel alloc] initWithFrame:frame];
stateLabel2.tag = StateTag2;
[cell.contentView addSubview:stateLabel2];
UILabel *capitalLabel = (UILabel *) [cell.contentView viewWithTag:CapitalTag];
UILabel *stateLabel = (UILabel *) [cell.contentView viewWithTag:StateTag];
UILabel *stateLabel1 = (UILabel *) [cell.contentView viewWithTag:StateTag1];
UILabel *stateLabel2 = (UILabel *) [cell.contentView viewWithTag:StateTag2];
capitalLabel.text=[a objectAtIndex:indexPath.row];
stateLabel.text = [b objectAtIndex:indexPath.row];
stateLabel1.text = [c objectAtIndex:indexPath.row];
stateLabel2.text = [d objectAtIndex:indexPath.row];
return cell;
-(IBAction)btnClicked:(id)sender
[self gainer];
-(IBAction)btnClicked1:(id)sender
[self looser];
-(void)gainer
arr1=[[NSMutableArray alloc]init];
arr2=[[NSMutableArray alloc]init];
a=[[NSMutableArray alloc]init];
b=[[NSMutableArray alloc]init];
c=[[NSMutableArray alloc]init];
d=[[NSMutableArray alloc]init];
NSURL *url=[NSURL URLWithString:@"http://ipad.idealake.com/default.aspx?id=G"];
NSURLRequest *req=[NSURLRequest requestWithURL:url];
[webView loadRequest:req];
//storing page data in string
mainstr=[[NSMutableString alloc] initWithContentsOfURL:url];
atarr=[mainstr componentsSeparatedByString:@"@"];
NSString *str2;
NSString *str3;
for(int i=0; i<[atarr count]-1; i++)
// NSLog(@"i=:%i",i);
NSMutableString *str = [atarr objectAtIndex:i];
if (str!= nil)
arr1=[str componentsSeparatedByString:@";"];
for (int k=0;k<[arr1 count];k++)
str2 = [arr1 objectAtIndex:k];
[arr2 addObject:str2];
else
//NSLog (@"Nill");
for(int l=0;l<[arr2 count]/4;l++)
str3=[arr2 objectAtIndex:4*l];
[a addObject:str3];
str3=[arr2 objectAtIndex:(4*l)+1];
[b addObject:str3];
str3=[arr2 objectAtIndex:(4*l)+2];
[c addObject:str3];
str3=[arr2 objectAtIndex:(4*l)+3];
[d addObject:str3];
-(void)looser
arr1=[[NSMutableArray alloc]init];
arr2=[[NSMutableArray alloc]init];
a=[[NSMutableArray alloc]init];
b=[[NSMutableArray alloc]init];
c=[[NSMutableArray alloc]init];
d=[[NSMutableArray alloc]init];
NSURL *url=[NSURL URLWithString:@"http://ipad.idealake.com/default.aspx?id=L"];
NSURLRequest *req=[NSURLRequest requestWithURL:url];
[webView loadRequest:req];
mainstr=[[NSMutableString alloc] initWithContentsOfURL:url];
atarr=[mainstr componentsSeparatedByString:@"@"];
NSString *str2;
NSString *str3;
for(int i=0; i<[atarr count]-1; i++)
NSMutableString *str = [atarr objectAtIndex:i];
if (str!= nil)
arr1=[str componentsSeparatedByString:@";"];
for (int k=0;k<[arr1 count];k++)
str2 = [arr1 objectAtIndex:k];
[arr2 addObject:str2];
else
//NSLog (@"Nill");
for(int l=0;l<[arr2 count]/4;l++)
str3=[arr2 objectAtIndex:4*l];
[a addObject:str3];
str3=[arr2 objectAtIndex:(4*l)+1];
[b addObject:str3];
str3=[arr2 objectAtIndex:(4*l)+2];
[c addObject:str3];
str3=[arr2 objectAtIndex:(4*l)+3];
[d addObject:str3];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
#pragma mark - View lifecycle
- (void)viewDidLoad
arr1=[[NSMutableArray alloc]init];
arr2=[[NSMutableArray alloc]init];
a=[[NSMutableArray alloc]init];
b=[[NSMutableArray alloc]init];
c=[[NSMutableArray alloc]init];
d=[[NSMutableArray alloc]init];
[self gainer];
[super viewDidLoad];
aTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen]applicationFrame] style:UITableViewStyleGrouped];
aTableView.dataSource = self;
aTableView.delegate = self;
aTableView.frame = CGRectMake(0, 10, 720, 500);
[self.view addSubview:aTableView];
[super viewDidLoad];
- (void)viewDidUnload
[super viewDidUnload];
- (void)viewWillAppear:(BOOL)animated
[super viewWillAppear:animated];
- (void)viewDidAppear:(BOOL)animated
[super viewDidAppear:animated];
- (void)viewWillDisappear:(BOOL)animated
[super viewWillDisappear:animated];
- (void)viewDidDisappear:(BOOL)animated
[super viewDidDisappear:animated];
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
// Return YES for supported orientations
return YES;
@end
更新
当我运行下面的代码时,我得到了这个输出。灰色阴影部分是我主要关心的问题。
提前致谢
【问题讨论】:
【参考方案1】:您需要通过调用reloadData
告诉您的UITableView
数据已更改。
【讨论】:
@GAMA 无论如何这是一场速度打字比赛:) Lolz... 但是还有 1 个问题。表格在框架内没有以完美的方式显示。 @GAMA 您可能想提出一个单独的问题,并附上一两个屏幕截图以更好地解释您所说的“不完美的方式”是什么意思。 我猜这不值得提出一个新问题。所以我已经上传了截图,如果你能帮助解决这个问题,那将会很有帮助。 @GAMA 我没有看到指向您正在运行的应用程序屏幕截图的链接,其中显示了错误的布局。您忘记添加图片链接了吗?【参考方案2】:在每次更改 url 并填充新数组后使用 [self.tableview reloadData]。
【讨论】:
我已经上传了截图,如果你能帮助解决这个问题,将会很有帮助。以上是关于动态填充 UITableView的主要内容,如果未能解决你的问题,请参考以下文章