UITableView如何一次显示两个不同的数组? [英] How UITableView show two different arrays one at a time?
问题描述
下面的代码有效,但不是我所希望的.我希望当我单击UIbutton时,它会自动更新UITableview中的新值而不是旧值.下面的代码仅在按下UIbuttons之后才有效,然后在滚动UITableview时起作用然后使用新值更新UItableview. 在我的应用程序中,我将UITableview用作mainclass的子类.如下图所示
The below Code works but not as i wish.i want that when i click UIbutton its automaically update the new value in UITableview instead of old value.Below Code works only when i press the UIbuttons and after that when i scroll the UITableview then it update the UItableview with new values. In my application i using UITableview as Subclass of my mainclass.as image show below
我在主类中添加Tableview就是这样的"testingViewController" 在testingViewController.h
I add Tableview in my Mainclass which is "testingViewController" like this way In testingViewController.h
#import "Inputtableview.h"
@interface testingViewController :UIViewController<UITableViewDelegate,UITableViewDataSource> {
Inputtableview *inputview;
IBOutlet UITableView *inputtbl;
}
@end
在testingViewController.m中
In testingViewController.m
- (void)viewDidLoad {
btn1bool=FALSE;
if (inputview == nil) {
inputview = [[Inputtableview alloc] init];
}
[inputtbl setDataSource:inputview];
[inputtbl setDelegate:inputview];
inputview.view = inputview.tableView;
}
现在可以使用按钮动作方法
Now in Button action method
-(IBAction)input:(id)sender
{
btn1bool=TRUE;
}
我的子类代码"inputtableview.m"显示如下
my Subclass code "inputtableview.m" is show below
- (void)viewDidLoad {
[super viewDidLoad];
listOfItems=[[NSMutableArray alloc] initWithObjects:@"Iceland",@"Greenland",@"Switzerland",
@"Norway",@"New Zealand",@"Greece",@"Italy",@"Ireland",nil];
array1 = [[NSMutableArray alloc] initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",@"G",@"H", nil] ;
}
#pragma mark -
#pragma mark Table View datasource methods
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
if (btn1bool) {
return [array1 count];
}
else {
return [listOfItems count];
}
[self.tableView reloadData];
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"CellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
}
NSLog(@"Row: %i", indexPath.row);
if (btn1bool) {
NSString *cellValue = [array1 objectAtIndex:indexPath.row];
cell.text = cellValue;
}
else {
NSString *cellValue = [listOfItems objectAtIndex:indexPath.row];
cell.text = cellValue;
}
return cell;
}
任何帮助都会得到帮助.
Any help will be appriated.
推荐答案
只需输入以下代码:
[inputtbl reloadData];
您需要在项目中进行一些更改,但我认为该项目仅用于测试.
There are a few things you need to change in your project, but I assume this project is just for testing stuff.
您希望在按下按钮后重新加载日期,因此您可以在IBAction中调用该方法.
You want the date to reload after you pressed the button, so you call the method in the IBAction.
-(IBAction)input:(id)sender
{
btn1bool=TRUE;
[inputview.tableView reloadData];
}
要在按下按钮时在两个数据源之间切换,可以更改为以下代码行:btn1bool=!btn1bool;
To switch between the 2 data sources when the button is pressed you can change to this line of code: btn1bool=!btn1bool;
(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
if (btn1bool) {
return [array1 count];
} else {
return [listOfItems count];
}
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
是正确的
这篇关于UITableView如何一次显示两个不同的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!