首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自两个不同UITableView的单个文本

来自两个不同UITableView的单个文本
EN

Stack Overflow用户
提问于 2012-04-20 13:08:56
回答 3查看 139关注 0票数 3

我有两个UITableview,如果我在每个表中选择一行,那么该特定行的textLabel.text应该显示在单个UIAlertview中。

如何组合两个表的textLabel.text并在一个UIAlertView中显示

有没有人能告诉我怎么做?

例如:一个显示A、B、C、D的表视图和另一个显示1、2、3、4的tableView。这两个表来自不同的类。现在假设如果我按下表1中的一行,我将得到textLabel.text为'A‘,如果我按下表2,我将在视图上得到textLabel.text为'1’,如果在table1中选择A,在表2中为1,我应该会得到一个AlertView,显示消息为'A1‘。

参考代码:

viewController.h

代码语言:javascript
复制
#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

.m:

代码语言:javascript
复制
    #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

table1:

代码语言:javascript
复制
#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

Table2:

代码语言:javascript
复制
#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

敬请指教

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-04-20 13:18:29

我不清楚什么时候你想要显示你的alertView,但这会对你有帮助

您需要具有用于放置标注文本的属性

所以在你的.h上

代码语言:javascript
复制
@ interface MyViewController :UIViewController {
 NSString *_stringTable1;
    NSString *_stringTable2;
}

@property (nonatomic, retain)  NSString *stringTable1;
@property (nonatomic, retain)  NSString *stringTable2;

在.m上

代码语言:javascript
复制
@synthesize stringTable1 = _stringTable1; 
@synthesize stringTable2 = _stringTable2; 
- (void) dealloc{
[_stringTable1 release];
[_stringTable2 release];
    [super dealloc];
}

所以在你的表委派上

代码语言:javascript
复制
 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    {

    if (tableView == table1) {
    self.stringTable1  = [myArray1 objectAtIndex: indexPath.row];   
    //call uiAlert, and place the stringTable1 on your message
 }
    if (tableView == table2) {
    self.stringTable2  = [myArray2 objectAtIndex: indexPath.row];   
        //call uiAlert, and place the stringTable2 on your message

 }

    }

当你调用你的uiAlertView时,对于消息,你可以像这样向他们展示

代码语言:javascript
复制
 [NSString stringWithFormat:@"%@ %@", self.stringTable1, self.stringTable2];
票数 2
EN

Stack Overflow用户

发布于 2012-04-20 13:19:38

//在第二个表格单元格点击事件时,将两个字符串合并为一个字符串...

//在didSelectRowAtIndexPath method..and中使用以下代码享受...

代码语言:javascript
复制
NSString *tempstring = [[NSString alloc]init];
   tempstring =yourlable.text;// your cell label...
   if (textField.tag == 1) {

    NSUserDefaults *stringsaver = [NSUserDefaults standardUserDefaults];

    if([stringsaver objectForKey:@"stringsaver"]== nil)
    {
        [stringsaver setObject:tempstring forKey:@"stringsaver"];
    }
    else
    {
        NSString *combinedstring = [stringsaver objectForKey:@"stringsaver"];
       //NSLog(@"==%@",combinedstring);

        UIAlertView *alertdata = [[UIAlertView alloc] 
                                  initWithTitle:@"Your Title" 
                                  message:combinedstring
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertdata show];
        [alertdata release];

       [stringsaver removeObjectForKey:@"stringsaver"];
    }
票数 1
EN

Stack Overflow用户

发布于 2012-04-20 14:22:50

试试这个-

代码语言:javascript
复制
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell1 = [tableView cellForRowAtIndexPath:indexPath];

    // get here the instance of otherTableView.
    UITableViewCell * cell2 = [otherTableView cellForRowAtIndexPath:indexPath];

   UILabel *Title1 = (UILabel *)[cell1 viewWithTag:1]; //for it you need to make the label with Tag 1
   UILabel *Title2 = (UILabel *)[cell2 viewWithTag:1]; //for it you need to make the label with Tag 1 
   NSString * str = [NSString stringWithFormat:@"%@ %@",[Title1 text],[Title2 text]];


  //fire the alert

   UIAlertView *alertdata = [[UIAlertView alloc] 
                              initWithTitle:@"Your Title" 
                              message:str
                              delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
    [alertdata show];

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10240588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档