草庐IT

iphone - 来自两个不同 UITableView 的单个文本

coder 2024-01-12 原文

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

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

谁能告诉我我该怎么做

EX:一个显示 A、B、C、D 的表格 View 和一个显示 1、2、3、4 的表格 View 。 这两张 table 来自不同的类(class)。现在假设如果我按表 1 中的一行,我将得到 textLabel.text 作为“A”,如果我按表 2,我将在 View 中得到 textLabel.text 为“1”,如果选择表 1 中的 A 和表中的 1 2 我应该得到一个显示消息为“A1”的 AlertView

引用代码:

viewController.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

.m:

    #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

请建议

最佳答案

当你想要显示 alertView 时,我不清楚, 但这对你有帮助

你需要有一个放置标签文本的属性

所以在你的 .h 上

@ interface MyViewController :UIViewController {
 NSString *_stringTable1;
    NSString *_stringTable2;
}

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

在你的 .m 上

@synthesize stringTable1 = _stringTable1; 
@synthesize stringTable2 = _stringTable2; 
- (void) dealloc{
[_stringTable1 release];
[_stringTable2 release];
    [super dealloc];
}

所以在你的表委托(delegate)上

 - (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 时,对于消息您可以像这样将它们一起显示

 [NSString stringWithFormat:@"%@ %@", self.stringTable1, self.stringTable2];

关于iphone - 来自两个不同 UITableView 的单个文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10240588/

有关iphone - 来自两个不同 UITableView 的单个文本的更多相关文章

  1. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  2. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  3. ruby - 这两个 Ruby 类初始化定义有什么区别? - 2

    我正在阅读一本关于Ruby的书,作者在编写类初始化定义时使用的形式与他在本书前几节中使用的形式略有不同。它看起来像这样:classTicketattr_accessor:venue,:datedefinitialize(venue,date)self.venue=venueself.date=dateendend在本书的前几节中,它的定义如下:classTicketattr_accessor:venue,:datedefinitialize(venue,date)@venue=venue@date=dateendend在第一个示例中使用setter方法与在第二个示例中使用实例变量之间是

  4. java - 为什么 ruby​​ modulo 与 java/other lang 不同? - 2

    我基本上来自Java背景并且努力理解Ruby中的模运算。(5%3)(-5%3)(5%-3)(-5%-3)Java中的上述操作产生,2个-22个-2但在Ruby中,相同的表达式会产生21个-1-2.Ruby在逻辑上有多擅长这个?模块操作在Ruby中是如何实现的?如果将同一个操作定义为一个web服务,两个服务如何匹配逻辑。 最佳答案 在Java中,模运算的结果与被除数的符号相同。在Ruby中,它与除数的符号相同。remainder()在Ruby中与被除数的符号相同。您可能还想引用modulooperation.

  5. ruby - 具有两个参数的 block - 2

    我从用户Hirolau那里找到了这段代码:defsum_to_n?(a,n)a.combination(2).find{|x,y|x+y==n}enda=[1,2,3,4,5]sum_to_n?(a,9)#=>[4,5]sum_to_n?(a,11)#=>nil我如何知道何时可以将两个参数发送到预定义方法(如find)?我不清楚,因为有时它不起作用。这是重新定义的东西吗? 最佳答案 如果您查看Enumerable#find的文档,您会发现它只接受一个block参数。您可以将它发送两次的原因是因为Ruby可以方便地让您根据它的“并行赋

  6. ruby-on-rails - 在 RSpec 中,如何以任意顺序期望具有不同参数的多条消息? - 2

    RSpec似乎按顺序匹配方法接收的消息。我不确定如何使以下代码工作:allow(a).toreceive(:f)expect(a).toreceive(:f).with(2)a.f(1)a.f(2)a.f(3)我问的原因是a.f的一些调用是由我的代码的上层控制的,所以我不能对这些方法调用添加期望。 最佳答案 RSpecspy是测试这种情况的一种方式。要监视一个方法,用allowstub,除了方法名称之外没有任何约束,调用该方法,然后expect确切的方法调用。例如:allow(a).toreceive(:f)a.f(2)a.f(1)

  7. ruby-on-rails - rspec - 我怎样才能让 "pendings"有我的文本而不仅仅是 "No reason given" - 2

    我有这个代码:context"Visitingtheusers#indexpage."dobefore(:each){visitusers_path}subject{page}pending('iii'){shouldhave_no_css('table#users')}pending{shouldhavecontent('Youhavereachedthispageduetoapermissionic错误')}它会导致几个待处理,例如ManagingUsersGivenapractitionerloggedin.Visitingtheusers#indexpage.#Noreason

  8. ruby-on-rails - 如何用不同的用户运行nginx主进程 - 2

    A/ctohttp://wiki.nginx.org/CoreModule#usermaster进程曾经以root用户运行,是否可以以不同的用户运行nginxmaster进程? 最佳答案 只需以非root身份运行init脚本(即/etc/init.d/nginxstart),就可以用不同的用户运行nginxmaster进程。如果这真的是你想要做的,你将需要确保日志和pid目录(通常是/var/log/nginx&/var/run/nginx.pid)对该用户是可写的,并且您所有的listen调用都是针对大于1024的端口(因为绑定(

  9. ruby - 可以正常中断的来自 Rake 的长时间运行的 shell 命令? - 2

    在几个项目中,我希望有一个类似rakeserver的rake任务,它将通过任何需要的方式开始为该应用程序提供服务。这是一个示例:task:serverdo%x{bundleexecrackup-p1234}end这行得通,但是当我准备停止它时,按Ctrl+c并没有正常关闭;它中断了Rake任务本身,它说rakeaborted!并给出堆栈跟踪。在某些情况下,我必须执行Ctrl+c两次。我可能可以用Signal.trap写一些东西来更优雅地中断它。有没有更简单的方法? 最佳答案 trap('SIGINT'){puts"Yourmessa

  10. ruby-on-rails - 将保存回调添加到单个 ActiveRecord 实例,可以吗? - 2

    是否可以为单个ActiveRecord实例添加回调?作为进一步的限制,这是继续使用库,所以我无法控制该类(除了对其进行猴子修补)。这或多或少是我想做的:defdo_something_creazymessage=Message.newmessage.on_save_call:do_even_more_crazy_stuffenddefdo_even_more_crazy_stuff(message)puts"Message#{message}hasbeensaved!Hallelujah!"end 最佳答案 你可以通过在创建对象后立

随机推荐