下面的代码工作,但不是我希望.我希望当我点击UI按钮时,它自动更新UITableview中的新值而不是旧值.Below代码只有当我按下UI按钮时才有效,之后我滚动UITableview然后更新具有新值的UItableview.
在我的应用程序中,我使用UITableview作为我的mainclass.as图像的子类,如下所示
在我的应用程序中,我使用UITableview作为我的mainclass.as图像的子类,如下所示
我在我的Mainclass中添加Tableview,就像这样“testingViewController”
在testingViewController.h中
#import "Inputtableview.h" @interface testingViewController :UIViewController<UITableViewDelegate,UITableViewDataSource> { Inputtableview *inputview; IBOutlet UITableView *inputtbl; } @end
在testingViewController.m中
- (void)viewDidLoad { btn1bool=FALSE; if (inputview == nil) { inputview = [[Inputtableview alloc] init]; } [inputtbl setDataSource:inputview]; [inputtbl setDelegate:inputview]; inputview.view = inputview.tableView; }
现在在Button动作方法中
-(IBAction)input:(id)sender { btn1bool=TRUE; }
我的子类代码“inputtableview.m”如下所示
- (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; }
将提供任何帮助.
解决方法
只需输入以下代码:
[inputtbl reloadData];
您希望在按下按钮后重新加载日期,因此您可以在IBAction中调用该方法.
-(IBAction)input:(id)sender { btn1bool=TRUE; [inputview.tableView reloadData]; }
要在按下按钮时在两个数据源之间切换,您可以更改为以下代码行: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是正确的