gpt4 book ai didi

ios - 如何在 iOS 中将 tableview 动态推送到 UIView?

转载 作者:行者123 更新时间:2023-11-29 10:36:35 28 4
gpt4 key购买 nike

i am displaying list content in table view now i want when i click one menu of list that menu content display in UIView on next screen . how to push UITableView to UIView dynamically in iOS.

      #import "SmsCategoryTitleTableViewController.h"
#import "CategoryMainWindowViewController.h"
#import "AppDelegate.h"
#import "FMDatabase.h"
#import "FMResultSet.h"
#import "SmsTitle.h"
#import "SMSCategory.h"
#import"smsDisplayViewController.h"

@interface SmsCategoryTitleTableViewController ()

@end

@implementation SmsCategoryTitleTableViewController
@synthesize theSearchBar,Id;
@synthesize theTableView;
@synthesize array;
@synthesize disableViewOverlay;
@synthesize Arrayobject;

- (void)viewDidLoad
{
[super viewDidLoad];
[self SearchBarCode];
[self GetTableData];

[self.tableView reloadData];
filteredContentList = [[NSMutableArray alloc] init];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(dismissKeyboard)];

[self.view addGestureRecognizer:tap];
// [self performSelector:@selector(push:) withObject:nil afterDelay:0.2f];
}


- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}


-(void)GetTableData
{
array = [[NSMutableArray alloc] init];

NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentDir = [docPaths objectAtIndex:0];
self.databasePath = [documentDir stringByAppendingPathComponent:@"SMS.sqlite"];

FMDatabase *database = [FMDatabase databaseWithPath:self.databasePath];
[database setLogsErrors:TRUE];
[database open];



NSString *anQuery = [[NSString alloc]initWithFormat:@"SELECT * FROM SMSnJokes
where CategoryId=%@",self.Id];

FMResultSet *results = [database executeQuery:anQuery];

while([results next])
{
SmsTitle *title=[[SmsTitle alloc]init];
title.Id = [results stringForColumn:@"Id"];
title.CategoryId = [results stringForColumn:@"CategoryId"];
title.Title = [results stringForColumn:@"Title"];
[array addObject:title];
// NSLog(@"SMS LIST %@",title.Title);

}


[database close];

}


-(void)SearchBarCode
{
self.disableViewOverlay = [[UIView
alloc]initWithFrame:CGRectMake(0.0f,44.0f,320.0f,0)];
self.disableViewOverlay.backgroundColor=[UIColor lightGrayColor];
self.disableViewOverlay.alpha = 0;

theSearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 5, 374.0f, 50)];
theSearchBar.delegate =self;
[self.tableView addSubview:theSearchBar];
self.navigationItem.title=@"SMS LIST";

[[self tableView] setTableHeaderView:theSearchBar];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:
(NSInteger)section {
// Return the number of rows in the section.
if (isSearching)
{
return [filteredContentList count];
}
else
{
return [array count];
}

}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:
(NSIndexPath *)indexPath
{
static NSString *CellIdentifier=@"Cell";
UITableViewCell *cell=[tableView
dequeueReusableCellWithIdentifier:CellIdentifier ];

if(!cell)
{


cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] ;
}

if (isSearching)
{
cell.textLabel.text = [filteredContentList objectAtIndex:indexPath.row];

}
else
{
SmsTitle *title = [array objectAtIndex:indexPath.row];
[cell.textLabel setText:[NSString stringWithFormat:@"%@ ",[title
valueForKey:@"Title"]]];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

return cell;

}
- (void)searchTableList
{
NSString *searchString =theSearchBar.text;
filteredContentList = [[NSMutableArray alloc]init];
[filteredContentList removeAllObjects];
for (SmsTitle *title in array)
{
NSString *tempStr = title.Title;
NSComparisonResult result = [tempStr compare:searchString options:
(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0,
[searchString length])];
if (result == NSOrderedSame)
{
[filteredContentList addObject:title.Title];
}
}


}




#pragma mark - Search Implementation


- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.theSearchBar resignFirstResponder];
}


- (void) dismissKeyboard
{
[self.theSearchBar becomeFirstResponder];
}


- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{

isSearching = YES;

[theSearchBar setShowsCancelButton:YES animated:YES];


}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSLog(@"Text change - %d",isSearching);

//Remove all objects first.
[filteredContentList removeAllObjects];


if([searchText length] != 0) {
isSearching = YES;
[self searchTableList];

}
else {
isSearching = NO;

}
[self.tableView reloadData];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
theSearchBar.text=nil;
[theSearchBar setShowsCancelButton:NO animated:YES];
[theSearchBar resignFirstResponder];
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[theSearchBar setShowsCancelButton: YES animated: YES];





[self searchTableList];
[searchBar resignFirstResponder];

  - (void)searchBar:(UISearchBar *)searchBar activate:(BOOL) active
{
self.theTableView.allowsSelection = !active;
self.theTableView.scrollEnabled = !active;

if (!active)
{

[disableViewOverlay removeFromSuperview];
[searchBar resignFirstResponder];
}
else
{
self.disableViewOverlay.alpha = 0;
[self.view addSubview:self.disableViewOverlay];

[UIView beginAnimations:@"FadeIn" context:nil];
[UIView setAnimationDuration:0.5];
self.disableViewOverlay.alpha = 0.6;
[UIView commitAnimations];
NSIndexPath *selected = [self.theTableView indexPathForSelectedRow];
if (selected)
{
[self.theTableView deselectRowAtIndexPath:selected
animated:NO];
}
}
[searchBar setShowsCancelButton:active animated:YES];
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

[self.view endEditing:YES];
[self.theSearchBar resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
textField.returnKeyType=UIReturnKeyDefault ;
return[textField resignFirstResponder];
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
*)indexPath
{
smsDisplayViewController *viewController1 = [[smsDisplayViewController alloc]
init];

[self.navigationController pushViewController:viewController1 animated:YES];

}

最佳答案

Ok got your point. I think you want to pass data of selected cell to next UIViewController. Assume, you want to pass cell title label and you have an array of objects. In yoursmsDisplayViewController.h

   #import <UIKit/UIKit.h>
@interface smsDisplayViewController : UIViewController
@property (strong, nonatomic) NSString *cellName;

现在在你的 SmsCategoryTitleTableViewController.m 你有 NSMutableArray *nameList;然后在你的 TableView 委托(delegate)方法中

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];

smsDisplayViewController *viewController1 = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginIdentifier"];

viewController1.cellName=[nameList objectAtIndex:indexPath.row];

[self.navigationController pushViewController:viewController1 animated:YES];
}

更新这是您在问题中首先发布的代码。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
smsDisplayViewController *viewController1 = [[smsDisplayViewController
alloc] initWithNibName:@"viewController1" bundle:nil];
[self.navigationController pushViewController:viewController1 animated:YES];
}

Now the wrong thing you are doing here isinitWithNibName:@"viewController1". If you are not using any nib file then your code should be

smsDisplayViewController *viewController1 = [[smsDisplayViewController alloc] init];

[self.navigationController pushViewController:viewController1 animated:YES];

关于ios - 如何在 iOS 中将 tableview 动态推送到 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26623208/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com