我想在UITableView中使用所选单元格的文本。它将在不同视图中的switch语句中使用。我该怎么办?
这是 UITableView :
的代码- (void)viewDidLoad
{
[super viewDidLoad];
_list = [[NSArray alloc] initWithObjects:@"Academic Advising Center", @"Academic Vice President", @"Administrative Services Vice President", @"Admissions", @"Advanced Standing and Transcript", @"Alcohol and Substance Abuse Programs", @"Allied Health Department", @"Alumni Network", nil];
_displayItems = [[NSMutableArray alloc] initWithArray:_list];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_displayItems count];
}
- (UITableViewCell *)tableView:(UITableView *) atableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
cell.textLabel.text = [_displayItems objectAtIndex:indexPath.row]; //TRY THIS ONE!!
return cell;
}
这是将使用UITableView中单元格文本的视图代码(现在,我在switch语句中使用单元格编号,但我想使用单元格的内容而不是单元格编号):
- (void)configureView
{
// Update the user interface for the detail item.
switch (_itemNumber)
{
case 0:
[_phoneButton setTitle:@"233-123-2133" forState:UIControlStateNormal];
[_emailButton setTitle:@"adf@dsf.sdf" forState:UIControlStateNormal];
self.title = @"Acdemic Adv. Center";
break;
case 1:
[_phoneButton setTitle:@"324-123-4231" forState:UIControlStateNormal];
[_emailButton setTitle:@"WERERW@wefff.edu" forState:UIControlStateNormal];
self.title = @"Acdemic VP";
break;
case 2:
[_phoneButton setTitle:@"232-222-3333" forState:UIControlStateNormal];
[_emailButton setTitle:@"eeeee@vfff.sss" forState:UIControlStateNormal];
self.title = @"Admin. Serv. VP";
break;
case 3:
[_phoneButton setTitle:@"111-222-3333" forState:UIControlStateNormal];
[_emailButton setTitle:@"eeeed@def.ed" forState:UIControlStateNormal];
self.title = @"Admissions";
break;
case 4:
[_phoneButton setTitle:@"343-333-2222" forState:UIControlStateNormal];
[_emailButton setTitle:@"eee@fff.www" forState:UIControlStateNormal];
self.title = @"Adv. Stand. & Trans.";
break;
case 5:
[_phoneButton setTitle:@"333-333-3333" forState:UIControlStateNormal];
[_emailButton setTitle:@"ffff@sss.aaa" forState:UIControlStateNormal];
self.title = @"Alc. & Subs. Abuse Prog.";
break;
case 6:
[_phoneButton setTitle:@"444-332-2222" forState:UIControlStateNormal];
[_emailButton setTitle:@"errvff@ede.def" forState:UIControlStateNormal];
self.title = @"Allied Health Dept.";
break;
case 7:
[_phoneButton setTitle:@"343-222-4444" forState:UIControlStateNormal];
[_emailButton setTitle:@"saff@fgd.eee" forState:UIControlStateNormal];
self.title = @"Alumni Network";
break;
default:
[_phoneButton setTitle:@"" forState:UIControlStateNormal];
[_emailButton setTitle:@"" forState:UIControlStateNormal];
self.title = @"";
break;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
_itemNumber = [[NSUserDefaults standardUserDefaults]integerForKey:@"Cell"];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
我知道:_itemNumber = [[NSUserDefaults standardUserDefaults] integerForKey:@“Cell”]; 将使用单元格的数量,我该如何使用内容?
答案 0 :(得分:1)
这实际上是非常糟糕的做法。我建议你将数据存储到带有多个NSDictinary的NSArray中,其中包含用户信息名称/电话/电子邮件。然后将NSArray中的每个对象映射到表视图中,因此具有特定索引的单元格将具有相同索引的数组。
答案 1 :(得分:0)
使用具有多个NSDictionary实例的NSArray作为数据存储(不要直接从此NSArray加载表)。这就是伊万·阿列克在另一个答案中的建议。
然后,在NSMutableArray中存储与搜索匹配的NSDictionary实例的索引(NSNumbers)。这样,搜索栏不会更改您的基本数据存储,只会更改此索引数组。
一些示例代码:
- (UITableViewCell *)tableView:(UITableView *) atableView cellForRowAtIndexPath: (NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
NSInteger listIndex = [[_searchResultIndices objectAtIndex:[indexPath row]] integerValue];
// assuming you create a dictionary where the title has the key title
cell.textLabel.title = [[_items objectAtIndex:listIndex] objectForKey:@"title"];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [_searchResultIndices count];
}