Ios TableView--->;详细视图
我想查看Ios TableView--->;详细视图,ios,xcode4,Ios,Xcode4,我想查看TableView上的组织名称,并在DetailView上查找更多信息。但我不知道哪里出错,DetailView找不到我的信息。以下是didSelectRow代码: TableView: - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { switch (section) { case 0: return 2;
TableView
上的组织名称,并在DetailView
上查找更多信息。但我不知道哪里出错,DetailView
找不到我的信息。以下是didSelectRow
代码:
TableView
:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
case 0:
return 2;
break;
case 1:
return 2;
break;
case 2:
return 2;
break;
}
return 0;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *sectionHeader = nil; if(section == 0) {
sectionHeader = @"Red Wine";
}
if(section == 1) {
sectionHeader = @"White Wine";
}
if(section == 2) {
sectionHeader = @"Sparkling Wine";
}
return sectionHeader;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
[cell.textLabel setNumberOfLines:3];
}
switch (indexPath.section) {
case 0:
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Black Wattle Mt Benson Merlot 2008";
cell.detailTextLabel.text = @"Mt Benson, South Australia.";
break;
case 1:
cell.textLabel.text = @"Two Hands Canny Butcher Barossa Valley Shiraz Grenache Mataro 2009";
cell.detailTextLabel.text = @"Barossa Valley, South Australia.";
break;
}
break;
case 1:
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Amberton Lizard Sauvignon Blanc Semillon 2011";
cell.detailTextLabel.text = @"South Eastern Australia.";
break;
case 1:
cell.textLabel.text = @"Vasse Felix Margaret River Chardonnay Margaret River";
cell.detailTextLabel.text = @"Western Australia.";
break; }
break;
case 2:
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Janisson Fils Brut Non Vintage Champagne";
cell.detailTextLabel.text = @"Champagne, France.";
break;
case 3:
cell.textLabel.text = @"Francois Montand Brut Blanc De Blancs NV";
cell.detailTextLabel.text = @"Premium French sparkling vineyard areas.";
break;
}
break;
}
return cell;
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *ViewController = [[DetailViewController alloc] init];
ViewController.Wine = [indexPath section];
[self.navigationController pushViewController:ViewController animated:YES];
}
- (void)viewDidLoad{
[super viewDidLoad];
switch (Wine) {
case 0:
switch (Wine) {
case 0:
self.navigationItem.title = @"Black Wattle Mt Benson Merlot 2008";
WineTextView.text = @"Alcohol: 14.5%\n"
"\n"
"Foods: Pan grilled fillet mignon with buttered mushrooms and silky mashed potatoes.\n";
break;
case 1:
self.navigationItem.title = @"Two Hands Canny Butcher Barossa Valley Shiraz Grenache Mataro 2009";
WineTextView.text = @"Alcohol: 14.2%\n"
"\n"
"Foods: Enjoy with beef stew and winter vegetables.\n";
break;
default:
break;
}
break;
case 1:
switch (Wine) {
case 0:
self.navigationItem.title = @"Amberton Lizard Sauvignon Blanc Semillon 2011";
WineTextView.text = @"Alcohol: 14.5%\n"
"\n"
"Foods: Pan grilled fillet mignon with buttered mushrooms and silky mashed potatoes.\n";
break;
case 1:
self.navigationItem.title = @"Vasse Felix Margaret River Chardonnay Margaret River";
WineTextView.text = @"Alcohol: 12%\n"
"\n"
"Foods: Enjoy with Chinese roast duck salad.\n";
break;
default:
break;
}
break;
case 2:
switch (Wine) {
case 0:
self.navigationItem.title = @"Janisson Fils Brut Non Vintage Champagne";
WineTextView.text = @"Alcohol: 12%\n"
"\n"
"Foods: Ideal aperitif style, to accompany canapés and hors doeuvres.\n";
break;
case 1:
self.navigationItem.title = @"Francois Montand Brut Blanc De Blancs NV";
WineTextView.text = @"Alcohol: 12%\n"
"\n"
"Foods: Apéritif, fish, and creamy dishes..\n";
break;
default:
break;
}
default:
break;
}
DetailView
:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
switch (section) {
case 0:
return 2;
break;
case 1:
return 2;
break;
case 2:
return 2;
break;
}
return 0;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
NSString *sectionHeader = nil; if(section == 0) {
sectionHeader = @"Red Wine";
}
if(section == 1) {
sectionHeader = @"White Wine";
}
if(section == 2) {
sectionHeader = @"Sparkling Wine";
}
return sectionHeader;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
[cell.textLabel setNumberOfLines:3];
}
switch (indexPath.section) {
case 0:
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Black Wattle Mt Benson Merlot 2008";
cell.detailTextLabel.text = @"Mt Benson, South Australia.";
break;
case 1:
cell.textLabel.text = @"Two Hands Canny Butcher Barossa Valley Shiraz Grenache Mataro 2009";
cell.detailTextLabel.text = @"Barossa Valley, South Australia.";
break;
}
break;
case 1:
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Amberton Lizard Sauvignon Blanc Semillon 2011";
cell.detailTextLabel.text = @"South Eastern Australia.";
break;
case 1:
cell.textLabel.text = @"Vasse Felix Margaret River Chardonnay Margaret River";
cell.detailTextLabel.text = @"Western Australia.";
break; }
break;
case 2:
switch (indexPath.row) {
case 0:
cell.textLabel.text = @"Janisson Fils Brut Non Vintage Champagne";
cell.detailTextLabel.text = @"Champagne, France.";
break;
case 3:
cell.textLabel.text = @"Francois Montand Brut Blanc De Blancs NV";
cell.detailTextLabel.text = @"Premium French sparkling vineyard areas.";
break;
}
break;
}
return cell;
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *ViewController = [[DetailViewController alloc] init];
ViewController.Wine = [indexPath section];
[self.navigationController pushViewController:ViewController animated:YES];
}
- (void)viewDidLoad{
[super viewDidLoad];
switch (Wine) {
case 0:
switch (Wine) {
case 0:
self.navigationItem.title = @"Black Wattle Mt Benson Merlot 2008";
WineTextView.text = @"Alcohol: 14.5%\n"
"\n"
"Foods: Pan grilled fillet mignon with buttered mushrooms and silky mashed potatoes.\n";
break;
case 1:
self.navigationItem.title = @"Two Hands Canny Butcher Barossa Valley Shiraz Grenache Mataro 2009";
WineTextView.text = @"Alcohol: 14.2%\n"
"\n"
"Foods: Enjoy with beef stew and winter vegetables.\n";
break;
default:
break;
}
break;
case 1:
switch (Wine) {
case 0:
self.navigationItem.title = @"Amberton Lizard Sauvignon Blanc Semillon 2011";
WineTextView.text = @"Alcohol: 14.5%\n"
"\n"
"Foods: Pan grilled fillet mignon with buttered mushrooms and silky mashed potatoes.\n";
break;
case 1:
self.navigationItem.title = @"Vasse Felix Margaret River Chardonnay Margaret River";
WineTextView.text = @"Alcohol: 12%\n"
"\n"
"Foods: Enjoy with Chinese roast duck salad.\n";
break;
default:
break;
}
break;
case 2:
switch (Wine) {
case 0:
self.navigationItem.title = @"Janisson Fils Brut Non Vintage Champagne";
WineTextView.text = @"Alcohol: 12%\n"
"\n"
"Foods: Ideal aperitif style, to accompany canapés and hors doeuvres.\n";
break;
case 1:
self.navigationItem.title = @"Francois Montand Brut Blanc De Blancs NV";
WineTextView.text = @"Alcohol: 12%\n"
"\n"
"Foods: Apéritif, fish, and creamy dishes..\n";
break;
default:
break;
}
default:
break;
}
至少
DetailView
中的代码有两个嵌套的switch语句,它们在同一变量Wine
上切换。例如,这导致“双手精明的屠夫巴罗萨谷”无法到达,因为葡萄酒
不能同时为零和一。也许您打算使用两个变量并存储节和行,以便在DetailView
中使用
在任何情况下,我都建议删除重复数据并将所有内容存储在一个位置。例如,如果您将所有葡萄酒信息存储在NSArray
中,则可以使用此数组检索数据,而无需使用大型case语句。这还允许将固定列表交换为从本地数据库甚至稍后从web检索的内容
例如,如果您创建了自己的类
WineInformation
,该类包含葡萄酒各个方面的属性(名称、酒精含量、推荐食物),您只需将所选行的WineInformation
对象传递给DetailView
,然后该视图将显示详细信息。通过这种方式,您不必在DetailView
中处理索引路径或任何类似内容,只需集中显示递过来的葡萄酒。为什么要编写两个相同的嵌套switch
块(在viewDidLoad中)?这段代码的一半永远不会被调用。为什么会发生这种情况?看起来您有内存泄漏(您分配了DetailViewController
,但从未释放它)。