Ios UITableView和UITableViewCells
我不确定我做错了什么,但如果有人能给我指出正确的方向,我将不胜感激。我正在xcode中创建一个应用程序。我现在有两个按钮,它们可以切换到不同的视图控制器。两个视图都由同一个类控制。类中定义了不同的视图。我还有两个数组,每个视图一个,其中填充了我想在视图中显示的信息。下面是viewcontrollers.m文件中的代码。如果我删除出现的断点,则两个按钮都起作用,但第二个按钮显示错误数组中的信息,但转到正确的视图控制器。我完全被难住了Ios UITableView和UITableViewCells,ios,objective-c,xcode,uitableview,Ios,Objective C,Xcode,Uitableview,我不确定我做错了什么,但如果有人能给我指出正确的方向,我将不胜感激。我正在xcode中创建一个应用程序。我现在有两个按钮,它们可以切换到不同的视图控制器。两个视图都由同一个类控制。类中定义了不同的视图。我还有两个数组,每个视图一个,其中填充了我想在视图中显示的信息。下面是viewcontrollers.m文件中的代码。如果我删除出现的断点,则两个按钮都起作用,但第二个按钮显示错误数组中的信息,但转到正确的视图控制器。我完全被难住了 #import "techTwoViewController.h
#import "techTwoViewController.h"
#import "maintInstrctViewController.h"
#import "showTechTipsViewController.h"
@interface techTwoViewController ()
@end
@implementation techTwoViewController
{
NSArray *maintInstrct;
NSArray *techTips;
}
@synthesize tableView;
@synthesize techTableView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Initialize table data
maintInstrct = [NSArray arrayWithObjects:@"One", @"Two", nil];
techTips = [NSArray arrayWithObjects:@"Three", @"Four", nil];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (NSInteger)techTableView:(UITableView *)techTableView numberOfRowsInSection:(NSInteger)section
{
return [techTips count];
}
- (UITableViewCell *)techTableView:(UITableView *)techTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTechTableIdentifier = @"TechTipsCell";
UITableViewCell *cell = [techTableView dequeueReusableCellWithIdentifier:simpleTechTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTechTableIdentifier];
}
cell.textLabel.text = [techTips objectAtIndex:indexPath.row];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [maintInstrct count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"MaintInstrctCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [maintInstrct objectAtIndex:indexPath.row];
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showMaintKitInstrct"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
maintInstrctViewController *destViewController = segue.destinationViewController;
destViewController.maintKitName = [maintInstrct objectAtIndex:indexPath.row];
}
else if ([segue.identifier isEqualToString:@"showTechTips"]) {
NSIndexPath *indexPath = [self.techTableView indexPathForSelectedRow];
showTechTipsViewController *destViewController = segue.destinationViewController;
destViewController.techTipName = [techTips objectAtIndex:indexPath.row];
}
}
@end
我知道这里发生了什么。您正在将此视图控制器指定为其拥有的两个
uitableview
的数据源。但是当您的techTableView
调用其数据源时,它无法知道您希望它调用自定义数据源方法。您需要为您的techTableView
提供自己的dataSource
。符合UITableViewDataSource协议并实现必要方法的单独类 风格提示:在Objective-C中,属性、方法和变量以小写开头,而类名以大写开头。此外,这个问题的标题令人困惑。当你说button时,这意味着UIButton与这个问题无关。您要处理的是UITableView和UITableViewCells。请修改这个问题的标题。这是一个糟糕的OO设计模式的经典例子。将您的表视图迁移到它们自己的类中,这样您就不会遇到@geraldWilliam在回答中强调的问题。旁注:实际使用UITableViewController
s,并将它们作为子视图添加到主视图中。@jsksma同意UITableViewControllers。我想我应该提到这一点,但我确实想表达,任何符合适当协议的对象都可以。