ios - 使用 UITableView 中单元格中的文本

标签 ios xcode uitableview switch-statement

我想使用 UITableView 中选定单元格的文本。它将用在不同 View 中的 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 中单元格文本的 View 代码(现在,我在 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"]; 将使用单元格的编号,如何使用内容?

最佳答案

对于这样的事情来说,这实际上是非常糟糕的方法。我建议您将数据存储到带有多个 NSDictinary 的 NSArray 中,其中将包含用户信息名称/电话/电子邮件。然后将 NSArray 中的每个对象映射到 TableView 中,因此具有特定索引的单元格会将数据放入具有相同索引的数组中。

关于ios - 使用 UITableView 中单元格中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14551277/

相关文章:

ios - 从 iPhone 相机获取图像路径使用 UIImagePickerController 返回 nil

ios - Swift CMMotion - 显示手机的角度

xcode - 为什么没有设置 Swift 编译器标志?

ios - UITableViewCell contentView 框架在编辑模式下向右移动太多

ios - 在自定义 UITableViewHeaderFooterView 上添加一个按钮

ios - UI警报 TableView 在ios7中不起作用

ios - 获取崩溃无法在 ios 中将自己添加为 subview

ios - cell.detailTextLabel.text 的问题(无重复)

swift - 我可以默认使用核心数据创建实体吗?

ios - Xcode 6.4 : External libraries not getting imported properly