iPhone Appの最近のブログ記事

勉強がてらiPhoneアプリでシンプルなRSSリーダーを作ってみたみました。
右のAO TESTと書かれたアイコンがそうです。

アイコンをタップすると左の画像ようにスライドして画面が切り替わります。
(今回はgizmodo japanさんのxmlを拝借しました。)

タイトルをタップすると横にスライドしフィードのタイトル一覧が表示されます。
(今見るとページのタイトルが若干違いますが気にしないでください。)

気になるタイトルをタップするとSafariが起動しウェブサイトが表示されます。

ActionScriptと比べて、これだけの機能を実装するのにどんだけ面倒なんだよってちょっと思ってしまいましたが、
慣れればどうってことなくなるだろうと思うのでコツコツやることにします。

以下XMLを読んで表示するソース。
(行ごとの解説は後ほど書きます。)

#import "ViewController2.h"


@implementation ViewController2

//! メモリワーニングをした際に呼ばれるメソッド
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)viewDidLoad{
	
	[super viewDidLoad];
	
	
	if(m_read_dat){
		[m_read_dat release];
	}
	if(m_table_dat){
		[m_table_dat release];
	}
	if(m_section_dic){
		[m_section_dic release];
	}
	if(m_index_ary){
		[m_index_ary release];
	}
	
	
	NSURL *url = [NSURL URLWithString:@"http://feeds.gizmodo.jp/rss/gizmodo/atom.xml"];
	NSXMLParser *ps = [[NSXMLParser alloc] initWithContentsOfURL:url];
	
	[ps setDelegate:(id)self];
	[ps setShouldProcessNamespaces:FALSE];
	[ps setShouldReportNamespacePrefixes:FALSE];
	[ps setShouldResolveExternalEntities:FALSE];
	
	
	m_read_dat = [[NSMutableArray alloc] init];
	m_readable = FALSE;
	[ps parse];
	[ps release];
	
	
	//Create Table
	NSMutableDictionary *indexed_name_dic = [[NSMutableDictionary alloc] init];
	
	self.title = @"gizmodo japan";
	
	for (NSDictionary *dic in m_read_dat){
	
		NSString	*name = [dic objectForKey:@"title"];
		NSString	*site = [dic objectForKey:@"blog_url"];
		NSString	*published = [dic objectForKey:@"published"];
		NSString	*group_key = [published substringWithRange:NSMakeRange(2,5)];
		
		NSMutableArray *group_name_ary = [indexed_name_dic objectForKey:group_key];
		
		
		if(group_name_ary == NULL){
			group_name_ary = [[NSMutableArray alloc] init];
			[indexed_name_dic setObject:group_name_ary forKey:group_key];
			[group_name_ary release];
		}
		
		NSDictionary *add_dic = [[NSDictionary alloc] initWithObjectsAndKeys:name,@"title",site,@"blog_url",nil];
		[group_name_ary addObject:add_dic];	
	}
	
	int		ct = 0;
	NSArray *ary = [[[indexed_name_dic allKeys] sortedArrayUsingSelector:@selector(compare:)] retain];
	
	m_index_ary = [[NSMutableArray alloc] init];
	for(ct = [ary count] - 1; ct >= 0; --ct){
		
		[m_index_ary addObject:[ary objectAtIndex:ct]];
		
	}
	
	[ary release];
	
	NSSortDescriptor   *sort_desc = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:NO];
	NSArray				*sort_desc_ary = [NSArray arrayWithObject:sort_desc];
	NSMutableArray		*temp = [[NSMutableArray alloc] init];
	
	for(NSString *index_char in m_index_ary){
		NSMutableArray *group_name_ary = [indexed_name_dic objectForKey:index_char];
		[group_name_ary sortUsingDescriptors:sort_desc_ary];
		
		NSDictionary *char_dic = [[NSDictionary alloc] initWithObjectsAndKeys:index_char,@"index",group_name_ary,@"name_ary",nil];
		[temp addObject:char_dic];
		[char_dic release];
	}
	[sort_desc release];	
	m_table_dat = temp;
}

//! テーブルビューのセクション数を返すメソッド
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return [m_table_dat count];
}

//! テーブルビューのセクションにある行の数を返すメソッド
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	NSDictionary	*dic = [m_table_dat objectAtIndex:section];
	NSArray			*ary = [dic objectForKey:@"name_ary"];
	
	return [ary count];
}

//! テーブルビューのセクションヘッダの文字列を返すメソッド
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	NSDictionary	*section_dic = [m_table_dat objectAtIndex:section];
	
	return [section_dic valueForKey:@"index"];
}

//! テーブルビューのセクションインデックスのタイトル文字列の配列を返すメソッド
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
	return [m_table_dat valueForKey:@"index"];
}

//! インデックスをタッチした際に呼ばれるメソッド
- (NSInteger)tableView:(UITableView *)tableView 
sectionForSectionIndexTitle:(NSString *)title 
			   atIndex:(NSInteger)index {
	return [m_index_ary indexOfObject:title];
}

//! テーブルにセルが表示される(出現する)際に呼ばれるメソッド
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Set up the cell...
	NSDictionary	*dic = [m_table_dat objectAtIndex:indexPath.section];
	NSArray			*ary = [dic objectForKey:@"name_ary"];
	NSDictionary	*name_dic = [ary objectAtIndex:indexPath.row];
	
	cell.textLabel.text = [name_dic objectForKey:@"title"];
	
    return cell;
}


//! セルが選択される直前に実行されるメソッド

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	static NSString *MyIdentifier = @"Cells";
	
	// Try to retrieve from the table view a now-unused cell with the given identifier
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	
	// If no cell is available, create a new one using the given identifier
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
	}
	
    // Set up the cell...
	NSDictionary	*dic = [m_table_dat objectAtIndex:indexPath.section];
	NSArray			*ary = [dic objectForKey:@"name_ary"];
	NSDictionary	*dic2 = [ary objectAtIndex:indexPath.row];
	
	// Safariを開いて、サイトに飛ぶ
	UIApplication *app = [UIApplication sharedApplication];
	[app openURL:[NSURL URLWithString:[dic2 objectForKey:@"blog_url"]]];
	
	return indexPath;
}

- (void)parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName 
	attributes:(NSDictionary *)attributeDict
{
	// 要素名を保存(タグの名前)
	m_element_name = elementName;
	// タグ開始、辞書追加、フラグON
	if([elementName isEqualToString:@"entry"]){
		NSMutableDictionary		*dic = [[NSMutableDictionary alloc] init];
		
		[m_read_dat addObject:dic];
		m_readable = TRUE;
	}
	// タグ
	if(m_readable && [elementName isEqualToString:@"link"]){
		int		index;
		
		// 配列のインデックスを取得
		if([m_read_dat count] == 0){
			return;
		}
		index = [m_read_dat count];
		index = index - 1;
		if(index <= 0){
			index = 0;
		}
		
		// 配列から辞書を取り出す
		NSMutableDictionary	*dic = [m_read_dat objectAtIndex:index];
		if(dic == nil){
			return;
		}
		
		// 辞書にURLを追加
		[dic setObject:[attributeDict valueForKey:@"href"] forKey:@"blog_url"];
	}
}

- (void)parser:(NSXMLParser *)parser 
 didEndElement:(NSString *)elementName 
  namespaceURI:(NSString *)namespaceURI 
 qualifiedName:(NSString *)qName{     
	// タグ終了、フラグOFF
	if([elementName isEqualToString:@"entry"]){
		m_readable = FALSE;
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
	int		index;
	
	// 配列のインデックスを取得
	if([m_read_dat count] == 0){
		return;
	}
	index = [m_read_dat count];
	index = index - 1;
	if(index <= 0){
		index = 0;
	}
	
	// 配列から辞書を取り出す
	NSMutableDictionary	*dic = [m_read_dat objectAtIndex:index];
	
	if(dic == nil){
		return;
	}
	
	// フラグ
	if(!m_readable){
		return;
	}
	
	// タグならば辞書に追加
	if([m_element_name isEqual:@"title"] || [m_element_name isEqual:@"published"]){
		[dic setObject:string forKey:m_element_name];
	}
	// 要素名を空にする
	m_element_name = @"";
}


@end
| | トラックバック(0)

今日からiPhoneアプリ開発を始めました。

iphone_app_hello_world
| | トラックバック(0)

このアーカイブについて

このページには、過去に書かれたブログ記事のうちiPhone Appカテゴリに属しているものが含まれています。

前のカテゴリはiPhoneです。

次のカテゴリはjavascriptです。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。

iPhone App: 月別アーカイブ