Ios 将字幕从uitableview推送到详细视图
我是个新手,我真的被卡住了,不知道该怎么办 我遵循了一个关于如何创建uitableview并使用nsmutablearray中的数据填充它的教程,在这里之前一切都很好 我想为手机“字幕”添加更多描述,比如我的应用程序中的数字 我使用了另一个可变数组并在tableview中显示它 现在,当我按下单元格时,它将进入详图视图,仅显示单元格标题 我不知道如何将字幕文本链接到另一个标签 请帮忙 这是我的主要任务 @属性(非原子,强)NSMutableArray*对象@财产 (非原子,强)NSMutableArray*数@属性(非原子, 强)NSMutableArray*结果@性质(非原子,强) NSMutableArray*结果编号 @属性(非原子,强)IBUISearchBar*搜索栏 这是我的主要任务Ios 将字幕从uitableview推送到详细视图,ios,objective-c,xcode5,Ios,Objective C,Xcode5,我是个新手,我真的被卡住了,不知道该怎么办 我遵循了一个关于如何创建uitableview并使用nsmutablearray中的数据填充它的教程,在这里之前一切都很好 我想为手机“字幕”添加更多描述,比如我的应用程序中的数字 我使用了另一个可变数组并在tableview中显示它 现在,当我按下单元格时,它将进入详图视图,仅显示单元格标题 我不知道如何将字幕文本链接到另一个标签 请帮忙 这是我的主要任务 @属性(非原子,强)NSMutableArray*对象@财产 (非原子,强)NSMutable
#import "main.h"
#import "DetailView.h"
@interface MasterTableViewController ()
@end
@implementation MasterTableViewController
- (NSMutableArray *)objects
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
return _objects;
}
- (NSMutableArray *)numbers
{
if (!_numbers) {
_numbers = [[NSMutableArray alloc] init];
}
return _numbers;
}
- (NSMutableArray *)results
{
if (!_results) {
_results = [[NSMutableArray alloc] init];
}
return _results;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Add some objects to our array
[self.objects addObject:@"YouTube"];
[self.objects addObject:@"Google"];
[self.objects addObject:@"Yahoo"];
[self.objects addObject:@"Apple"];
[self.objects addObject:@"Amazon"];
[self.objects addObject:@"Bing"];
[self.objects addObject:@"Udemy"];
[self.objects addObject:@"Flickr"];
[self.numbers addObject:@"1"];
[self.numbers addObject:@"2"];
[self.numbers addObject:@"3"];
[self.numbers addObject:@"4"];
[self.numbers addObject:@"5"];
[self.numbers addObject:@"6"];
[self.numbers addObject:@"7"];
[self.numbers addObject:@"8"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)searchThroughData
{
self.results = nil;
NSPredicate *resultsPredicate = [NSPredicate predicateWithFormat:@"SELF contains [search] %@", self.searchBar.text];
self.results = [[self.objects filteredArrayUsingPredicate:resultsPredicate] mutableCopy];
}
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self searchThroughData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == self.tableView) {
return self.objects.count;
} else {
[self searchThroughData];
return self.results.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
if (tableView == self.tableView) {
cell.textLabel.text = self.objects[indexPath.row];
cell.detailTextLabel.text = self.numbers[indexPath.row];
} else {
cell.textLabel.text = self.results[indexPath.row];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (self.searchDisplayController.isActive) {
[self performSegueWithIdentifier:@"ShowDetail" sender:self];
}
}
if ([[segue identifier] isEqualToString:@"ShowDetail"]) {
NSString *object = nil;
NSString *numbers = nil;
NSIndexPath *indexPath = nil;
if (self.searchDisplayController.isActive) {
indexPath = [[self.searchDisplayController searchResultsTableView] indexPathForSelectedRow];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
object = cell.textLabel.text;
numbers = cell.detailTextLabel.text;
} else {
indexPath = [self.tableView indexPathForSelectedRow];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
object = cell.textLabel.text;
numbers = cell.detailTextLabel.text;
}
[[segue destinationViewController] setDetailLabelContents:object];
}
}
@end
这是我的详细视图
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController
@property (strong, nonatomic) NSString *detailLabelContents;
@property (strong, nonatomic) NSString *numberdetailLabelContents;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@property (weak, nonatomic) IBOutlet UILabel *numberdetailLabel;
我希望有人能帮助你你在prepareForSegue:中的这部分代码很脆弱:
if (self.searchDisplayController.isActive) {
indexPath = [[self.searchDisplayController searchResultsTableView] indexPathForSelectedRow];
object = self.results[indexPath.row];
} else {
indexPath = [self.tableView indexPathForSelectedRow];
object = self.objects[indexPath.row];
}
它是脆弱的,因为您是从结果数组中获取详细文本,而不是从单元格本身的详细文本标签中获取。尝试获取指向选定单元格的指针,并将其detailTextLabel传递给detailViewController。确保在MasterTableViewController中为tableView设置了IBOutlet。在本例中,我将其称为resultsTableView
if (self.searchDisplayController.isActive) {
indexPath = [[self.searchDisplayController searchResultsTableView] indexPathForSelectedRow];
UITableViewCell *cell = [self.resultsTableView cellForRowAtIndexPath:indexPath];
object = cell.detailTextLabel.text;
} else {
indexPath = [self.tableView indexPathForSelectedRow];
UITableViewCell *cell = [self.resultsTableView cellForRowAtIndexPath:indexPath];
object = cell.detailTextLabel.text;
}
DetailViewController *detailVC = [segue destinationViewController];
detailVC.detailLabelContents = numbers;
让我们知道这是否有效 您已经在将细节标签字符串传递给细节控制器,只需对self.numbersArray执行相同的操作,将该值传递给numberdetailLabelContents。我按照您所说的做了,现在我正在推单元格文本而不是数组值。。。。很好,但我仍然不知道如何将detailTextLable推送到detailviewcontrolleri我在segue中用新代码编辑了这个问题。。。但我仍然不知道如何将“number”变量推送到detailviewcontroller。。请在更新代码的这一行提供帮助
[[segue destinationViewController]setDetailLabelContents:object]代码>看起来您应该将对象更改为数字。我还添加了DetailViewController*detailVC=[segue destinationViewController];detailVC.detailLabelContents=编号代码>到我的代码示例的末尾。
if (self.searchDisplayController.isActive) {
indexPath = [[self.searchDisplayController searchResultsTableView] indexPathForSelectedRow];
UITableViewCell *cell = [self.resultsTableView cellForRowAtIndexPath:indexPath];
object = cell.detailTextLabel.text;
} else {
indexPath = [self.tableView indexPathForSelectedRow];
UITableViewCell *cell = [self.resultsTableView cellForRowAtIndexPath:indexPath];
object = cell.detailTextLabel.text;
}
DetailViewController *detailVC = [segue destinationViewController];
detailVC.detailLabelContents = numbers;