我是新手,我真的被卡住了,不知道该怎么做
我遵循了一个关于如何制作一个uitableview的教程,并用来自nsmutablearray的数据填充它一切都很好,直到这里
我想为我的应用添加更多关于单元格“字幕”的描述 我使用另一个可变数组并在tableview中显示
现在,当我按下单元格时,它会转到详细视图,只显示单元格标题
我不知道如何将字幕文本链接到另一个标签
请帮助
这是我的main.h
@property(非原子,强)NSMutableArray *对象; @属性 (非原子的,强大的)NSMutableArray *数字; @property(非原子的, strong)NSMutableArray *结果; @property(非原子,强) NSMutableArray * resultsnumber;
@property(非原子,强)IBOutlet UISearchBar * searchBar;
这是我的main.m
#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;
这是我的详细视图.m
#import "DetailView.h"
#import "main.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Set the label text
self.detailLabel.text = self.detailLabelContents;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我希望有人可以提供帮助
答案 0 :(得分:0)
您在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中将IBOutlet设置为tableView。我将在此示例中将其命名为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;
如果有效,请告诉我们!