Ya~~ 成功將首頁上的所有問答列表顯示在iPhone上了!
哈哈哈~~ 經過不斷地跟Google大師學習,這幾天來天天爆肝,總算有了初步的成果!已經能在iPhone上把文章標題列表出來囉!請看:
我用了最簡單的方法
先自訂一個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
新增一個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;
}
大功告成!