我有两个 UITableview,如果我在每个表中选择一行,则textLabel.text
该特定行的应该显示在单个 UIAlertview 中。
如何组合textLabel.text
两个表并在一个 UIAlertView 中显示
谁能让我知道我该怎么做
例如:一个显示 A、B、C、D 的表格视图和另一个显示 1、2、3、4 的 tableView。这两张桌子来自不同的班级。现在假设如果我在表 1 中按一行,我将得到 textLabel.text 作为“A”,如果我按表 2,如果在 table1 中选择 A 并在表中选择 1,则现在在视图上将 textLabel.text 作为“1” 2 我应该得到一个 AlertView 显示消息为“A1”
参考代码:
视图控制器.h
#import <UIKit/UIKit.h>
#import "FirstTVContoller.h"
#import "SecondTVController.h"
@interface TwoTableViewsViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>{
FirstTVContoller *firstController;
SecondTVController *secondController;
IBOutlet UITableView *firstTable;
IBOutlet UITableView *secondTable;
NSString *stringTable1;
NSString *stringTable2;
NSArray * myArray1;
NSArray * myArray2;
}
@property (nonatomic, retain) NSString *stringTable1;
@property (nonatomic, retain) NSString *stringTable2;
@property (nonatomic, retain) NSArray * myArray1;
@property (nonatomic, retain) NSArray * myArray2;
@end
米:
#import "TwoTableViewsViewController.h"
@implementation TwoTableViewsViewController
@synthesize stringTable1 = stringTable1;
@synthesize stringTable2 = stringTable2;
@synthesize myArray1,myArray2;
- (void)viewDidLoad {
[super viewDidLoad];
if (firstController == nil) {
firstController = [[FirstTVContoller alloc] init];
}
if (secondController == nil) {
secondController = [[SecondTVController alloc] init];
}
[firstTable setDataSource:firstController];
[secondTable setDataSource:secondController];
[firstTable setDelegate:firstController];
[secondTable setDelegate:secondController];
firstController.view = firstController.tableView;
secondController.view = secondController.tableView;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
if (tableView == firstTable) {
self.stringTable1 = [myArray1 objectAtIndex: indexPath.row];
//call uiAlert, and place the stringTable1 on your message
if (tableView == secondTable) {
self.stringTable2 = [myArray2 objectAtIndex: indexPath.row];
//call uiAlert, and place the stringTable2 on your message
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"hi" message:[NSString stringWithFormat:@"%@ %@", self.stringTable1, self.stringTable2] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[firstController release];
[secondController release];
[firstTable release];
[secondTable release];
[stringTable1 release];
[stringTable2 release];
[super dealloc];
}
@end
表格1:
#import <Foundation/Foundation.h>
@interface FirstTVContoller : UITableViewController <UITableViewDataSource, UITableViewDelegate>{
NSMutableArray *items;
}
@end
#import "FirstTVContoller.h"
#import "SecondTVController.h"
@implementation FirstTVContoller
-(void) loadView
{
if (items == nil) {
items = [[NSMutableArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"6",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",nil] retain];
}
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return [items count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewStylePlain reuseIdentifier:@"MyIdentifier"];
}
cell.textLabel.text = [NSString stringWithFormat:@"1.%@" ,[items objectAtIndex:indexPath.row]];
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *stringVariable = cell.textLabel.text;
NSLog(@"%@",stringVariable);
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if(editingStyle == UITableViewCellEditingStyleDelete) {
//Delete the object from the table.
[items removeObjectAtIndex:indexPath.row];
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
-(void) dealloc
{
[items release];
[super dealloc];
}
@end
表2:
#import <Foundation/Foundation.h>
@interface SecondTVController : UITableViewController <UITableViewDataSource, UITableViewDelegate>{
int numberOfCells;
}
@end
#import "SecondTVController.h"
@implementation SecondTVController
-(void) viewDidLoad
{
numberOfCells = 20;
}
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger) tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
{
return numberOfCells;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellSelectionStyleNone reuseIdentifier:@"MyIdentifier"];
}
cell.textLabel.text = [NSString stringWithFormat:@"2.%d", indexPath.row];
return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
NSString *stringVariable = cell.textLabel.text;
NSLog(@"%@",stringVariable);
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)tableView:(UITableView *)tv commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath {
if(editingStyle == UITableViewCellEditingStyleDelete) {
//Delete the object from the table.
numberOfCells -=1;
[tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
}
}
@end
请建议