iT邦幫忙

DAY 8
18

我愛吃蘋果系列 第 8

 [iPhone程式設計 之 iT邦幫忙 App] iT邦幫忙App UI 第一版 

Ya~~ 成功將首頁上的所有問答列表顯示在iPhone上了!
哈哈哈~~ 經過不斷地跟Google大師學習,這幾天來天天爆肝,總算有了初步的成果!已經能在iPhone上把文章標題列表出來囉!請看:

我用了最簡單的方法

  1. 先自訂一個Class,把文章的標題、作者等資訊給放起來。然後在parse HTML的時後,把一個個的物件給放到一個陣列裡,等等好餵給UITableView。

    @interface Item : NSObject {
    NSString *subtitleType;
    NSString *subtitle;
    NSString *user;
    NSString *timeDesc;
    }

    @property (readwrite, copy) NSString *subtitleType;
    @property (readwrite, copy) NSString *subtitle;
    @property (readwrite, copy) NSString *user;
    @property (readwrite, copy) NSString *timeDesc;

    @end

  2. 新增一個UITableViewController,並在專案預設的Delegate裡宣告、初始化、加入到主Window中。

MyTableViewController.h

#import <UIKit/UIKit.h>


@interface MyTableViewController : UITableViewController {
    NSMutableArray *items;
}

@property (readwrite, retain) NSMutableArray *items;
@end

iThelpDelegate.h

#import <UIKit/UIKit.h>
#import "MyTableViewController.h"

@interface iThelpAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;

    MyTableViewController *tableViewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

iThelpDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    
    // Override point for customization after application launch.
    [window makeKeyAndVisible];
    tableViewController = [[MyTableViewController alloc] init];
    UIView *tableView = tableViewController.view;
    [tableView setFrame:CGRectMake(0, 110, window.frame.size.width, window.frame.size.height - 110)];
    [window addSubview:tableViewController.view];
    
	return YES;
}

- (void)dealloc {
    [tableViewController release];
    [window release];
    [super dealloc];
}

在controller裡,把資料取回來,並放在陣列裡。

- (void)viewDidLoad {
    [super viewDidLoad];

    NSData *htmlData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:@"http://ithelp.ithome.com.tw"]];
    TFHpple *xpathParser = [[TFHpple alloc] initWithHTMLData:htmlData];
    
    NSArray *subtitleTypes  = [xpathParser search:@"//div[@class='hp_box']//li[@class='subTitle']"];
    int count = [subtitleTypes count];
    NSLog(@"Total questions: %d", count);
    
    NSArray *subtitles  = [xpathParser search:@"//div[@class='hp_box']//li[@class='subTitle']//a"];
    NSArray *userDescribes  = [xpathParser search:@"//div[@class='hp_box']//li[@class='user']"];
    NSArray *users  = [xpathParser search:@"//div[@class='hp_box']//li[@class='user']//a"];
    
    items = [[NSMutableArray alloc] init];
    for(int i = 0; i < count; i++){
        Item *item = [[Item alloc] init];
        item.subtitleType = [[subtitleTypes objectAtIndex:i] content];
        item.subtitle = [[subtitles objectAtIndex:i] content];
        item.user = [[users objectAtIndex:i] content];
        item.timeDesc = [[userDescribes objectAtIndex:i] content];
        
        [items addObject:item];
        [item release];
    }
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

最後在controller裡實作UITableView一定要的兩個方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [items count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    Item *item = (Item*)[items objectAtIndex:indexPath.row];
    cell.textLabel.text = [[NSString alloc] initWithFormat:@"%@ %@",item.subtitleType, item.subtitle];
    
    return cell;
}

大功告成!


上一篇
 [iPhone程式設計 之 iT邦幫忙 App] 把iT邦幫忙首頁HTML讀入程式裡 
下一篇
 [iPhone程式設計 之 iT邦幫忙 App] 打造自己的 UITableViewCell 
系列文
我愛吃蘋果30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言