0

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"]; セルの番号を使用しますが、内容はどのように使用できますか?

4

2 に答える 2

1

これは実際には、このようなことを行うための非常に悪いアプローチです。複数の NSDictinary を使用して NSArray にデータを保存することをお勧めします。これには、ユーザー情報名/電話/電子メールが含まれます。次に、NSArray のすべてのオブジェクトをテーブル ビューにマップします。そのため、特定のインデックスを持つセルには、同じインデックスを持つ配列にデータが含まれます。

于 2013-01-27T20:09:14.453 に答える
0

複数の NSDictionary インスタンスを持つ NSArray をデータ ストアとして使用します (ただし、この NSArray からテーブルを直接ロードしないでください)。これは、Ivan Alek が別の回答で示唆していることです。

次に、検索に一致する NSDictionary インスタンスのインデックス (NSNumbers) を NSMutableArray に格納します。そうすれば、基本的なデータ ストアは検索バーによって変更されず、このインデックスの配列のみが変更されます。

サンプルコード:

- (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];
}
于 2013-01-27T21:35:27.150 に答える