私はiOSにかなり慣れていませんが、そこに到達していますが、問題に遭遇しました。使用する構文がわからないだけだと確信しています。
UITableview に辞書の配列を入力し、辞書内の特定の項目を使用して 2 つのセクションに入力するにはどうすればよいですか? たとえば、辞書の配列である Web サイトから plist ファイルを読み取ります。ディクショナリ項目には「タイプ」という識別子があります。2 つの異なる「タイプ」変数があります。「リンク」を示すものと「表示」を示すものです。私がやろうとしているのは、あるセクションに「リンク」、別のセクションに「表示」があるセクション化されたテーブルを持つことです。
配列全体を読み取って単一のセクションに入れるコードを次に示しますが、どうすれば2つのセクションに入れることができますか?
これが plist のサンプルです。「タイプ」に注意してください。1 つは「weblink」で、もう 1 つは「show」です。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>Title</key>
<string>Faceboook</string>
<key>Type</key>
<string>weblink</string>
<key>MapAddress</key>
<string></string>
<key>Address</key>
<string></string>
<key>Date</key>
<string></string>
<key>Link</key>
<string>http://www.facebook.com/myfacebook</string>
</dict>
<dict>
<key>Title</key>
<string>Bernardo Winery</string>
<key>Type</key>
<string>show</string>
<key>MapAddress</key>
<string>321+Lombard+St,+San+Francisco,+CA</string>
<key>Address</key>
<string>321 Lombard St, San Francisco, CA</string>
<key>Date</key>
<string>Nov 2nd and 3rd.</string>
<key>Link</key>
<string></string>
</dict>
</array>
</plist>
ここで、plist を NSMutableArray TableData に読み込みます。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self)
{
NSError *error = nil;
NSString *URLString = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.mywebsite.com/ShowList.plist"]
encoding:NSUTF8StringEncoding
error:&error];
URLString = nil;
if(error == nil)
{
TableData = [[NSMutableArray alloc]initWithContentsOfURL:[NSURL URLWithString:@"http://www.mywebsite.com/ShowList.plist"]];
}
else
{
NSString *errorString = @"No internet connection, these links may not work!";
UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error"
message:errorString
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[av show];
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"ShowList" ofType: @"plist"];
TableData = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
}
}
return self;
}
そして、ここで TableData が tableView に供給されます。
-(UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tv dequeueReusableCellWithIdentifier:@"cell"];
// NSLog(@"The count: %i", [TableData count]);
// NSLog(@"Terms array %@",TableData);
// NSLog(@"value at index %i is %@", 1, [TableData objectAtIndex:1]);
NSDictionary *tmpDic = [[NSDictionary alloc] initWithDictionary:[TableData objectAtIndex:[indexPath row]] copyItems:YES];
if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
}
[[cell textLabel] setText:[tmpDic objectForKey:@"Title"]];
return cell;
}
よろしくお願いいたします。ウェンデル
編集:私は以下を実装しました-
if ([[tmpDic objectForKey:@"Type"] isEqualToString:@"weblink"]) {
if(indexPath.section==0)
{
[[cell textLabel] setText:[tmpDic objectForKey:@"Title"]];
}
}
if ([[tmpDic objectForKey:@"Type"] isEqualToString:@"show"]) {
if(indexPath.section==1)
{
[[cell textLabel] setText:[tmpDic objectForKey:@"Title"]];
}
}
しかし、次のことが起こっています。