我有一個UITableView和另一個視圖。另一個視圖有一個UITextView,我想用不同的文本填充,這取決於表視圖中的哪一行被選中。我知道如何爲所有不同的文本選項創建不同的視圖。如果我這樣做了,我可以在該行被點擊時打開該視圖。我只是想避免創建一堆不同的視圖,只是使用不同的文本加載不同的文本,具體取決於表視圖中的行。我想我只需要知道具有文本視圖的視圖如何從表視圖訪問indexPath.row。有什麼建議麼?當UITableView行被選中時用特定信息填充UITextView
編輯:這是我的代碼。
LHRRoster.m
#import "LHRRoster.h"
#import "CustomCellBackground.h"
#import "OnMyHonor.h"
#import "AVA.h"
#import "Receivers.h"
#import "BandDetailView.h"
@interface LHRRoster()
@end
@implementation LHRRoster
{
NSMutableArray *mbTableData;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
mbTableData = [NSMutableArray arrayWithObjects:@"Aesthetics Versus Architecture", @"On My Honor", @"Receivers", @"Skyscraper Stereo", @"California", @"Late Ones", @"Uh-Huh Baby Yeah", @"Danielle Bouchard", @"Chris Karrer", @"Joey Blue", @"The Codas", @"Your Favorite Hero", @"Nixon", @"Skam Impaired", @" SaySomethingHugeOnFire", @"Adore Me Not", @"Common Collective", @"The Royal Tees", @"The Gravetones", @"Bush League", @"Rock & Roll Television", @" Tastyface", @"The Lips", @"Mercy Academy", @"Audiostrobelight", nil];
self.title = @"LHR Roster";
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [mbTableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *mbTableIdentifier = @"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:mbTableIdentifier];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:mbTableIdentifier];
cell.textLabel.font=[UIFont systemFontOfSize:14.0];
}
cell.backgroundView = [[CustomCellBackground alloc] init];
cell.selectedBackgroundView = [[CustomCellBackground alloc] init];
cell.textLabel.backgroundColor = [UIColor clearColor];
cell.textLabel.highlightedTextColor = [UIColor darkGrayColor];
cell.textLabel.text = [mbTableData objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BandDetailView *detailView = [[BandDetailView alloc]initWithNibName:@"BandDetailView" bundle:nil];
if (indexPath.row == 0)
{
detailView.title = @"Aesthetics vs Architecture";
UIImage *image = [UIImage imageNamed: @"AVA.png"];
[detailView.bandPic setImage:image];
NSString *bio = @"";
[detailView.bandInfo setText:bio];
[self.navigationController pushViewController:detailView animated:YES];
}
}
@end
看一看[本教程(http://www.iphonesdkarticles.com/2009/01/uitableview-loading-detail-view.html)和修改,以滿足您的需要的代碼。 – Greg