2012-11-05 99 views
1

我已經創建了一個簡單的rss閱讀器,填充tableviewcell viewdidLoad沒有問題。我的問題是每當我點擊我的按鈕tableview不會改變。我試着用按鈕touchup重新加載裏面,沒有任何事情發生。 Xcode和iOS編程相當新穎,所以任何幫助都很棒。使按鈕填充UITableView時按下

.m文件

#import "ViewController.h" 
#import "KMXMLParser.h" 
#import "WebViewController.h" 
#import "SportsViewController.h" 

@interface ViewController() 
@end 

@implementation ViewController 
{ 
    NSArray *loadData; 
    NSURL *thumbnails; 
} 

@synthesize parseResults=_parseResults; 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    KMXMLParser *parser = [[KMXMLParser alloc] initWithURL:@"http://www.daytonastate.edu/rss/it.xml" delegate:nil]; 

    _parseResults = [parser posts]; 
    [self refreshFeed]; 
     // Do any additional setup after loading the view, typically from a nib. 
} 

-(void) refreshInvoked:(id)sender forState:(UIControlState)state 
{ 
    [self refreshFeed]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 

-(void)refreshFeed 
{ 
    KMXMLParser *parser = [[KMXMLParser alloc] initWithURL:@"http://www.daytonastate.edu/rss/it.xml" delegate:nil]; 

    _parseResults = [parser posts]; 
} 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"DataCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil) 
    { 

     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier]; 
    }// Configure the cell... 
     cell.textLabel.text = (self.parseResults)[indexPath.row][@"title"]; 

    cell.detailTextLabel.text = (self.parseResults)[indexPath.row][@"summary"]; 

    [self reloadData]; 

    return cell; 
} 
#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    WebViewController *vc = [[WebViewController alloc] init]; 

    vc.url = [NSURL URLWithString:(self.parseResults)[indexPath.row][@"link"] ]; 

    [self.navigationController pushViewController:vc animated:YES]; 

    //Makes sure function clicks to reader feed. 

    // Navigation logic may go here. Create and push another view controller. 
} 

-(IBAction)pressBtn:(id)sender 
{ 
    UIButton *button= [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

    if (button.tag==1) 
    { 
     NSLog(@"Press button 1"); 
     KMXMLParser *parser = [[KMXMLParser alloc] initWithURL:@"http://www.daytonastate.edu/rss/cea.xml" delegate:nil]; 

     _parseResults = [parser posts]; 

     [self reloadData]; 
    } 
    if (button.tag==2) 
    { 
     NSLog(@"Press button 2"); 
     [self EventsBtn:nil]; 
    } 
} 

- (IBAction)NewsBtn:(id)sender 
{ 
     KMXMLParser *parser = [[KMXMLParser alloc] initWithURL:@"http://rss.cnn.com/rss/cnn_topstories.rss" delegate:nil]; 

     _parseResults = [parser posts]; 
    NSLog(@"reload happened"); 
    // self.printMessage = [[PrintHello alloc] init]; // EDIT: THIS LINE WAS MISSING NOW IT WORKS 
    //[self.printMessage Print]; 

    NSLog(@"NewsBtn Pressed"); 
} 

- (IBAction)SportsBtn:(id)sender 
    { 
     KMXMLParser *parser = [[KMXMLParser alloc] initWithURL:@"http://www.daytonastate.edu/rss/it.xml" delegate:nil]; 

     _parseResults = [parser posts]; 
    } 

- (IBAction)EventsBtn:(id)sender 
{ 
    KMXMLParser *parser = [[KMXMLParser alloc] initWithURL:@"http://www.daytonastate.edu/rss/events.xml" delegate:nil]; 
    _parseResults = [parser posts]; 
    NSLog(@"eventsBtn Pressed"); 
} 

- (IBAction)WeatherBtn:(id)sender 
    { 
     KMXMLParser *parser = [[KMXMLParser alloc] initWithURL:@"http://w1.weather.gov/xml/current_obs/KDAB.rss" delegate:nil]; 

     _parseResults = [parser posts]; 
    } 
@end 
+1

在'cellForRowAtIndexPath'實現中刪除'[self reloadData];'是沒有意義的。 – Till

+0

...下一次,請發佈不包含如此大量空行和不相關內容的代碼 - 我試着爲您修復這個問題。 – Till

+0

對不起! – user1794601

回答

1

在你pressBtn:方法,替換此行:

UIButton *button= [UIButton buttonWithType:UIButtonTypeRoundedRect]; 

有:

UIButton *button = (UIButton *)sender; 

這是假設在IB您已經迷上了pressBtn:方法到合適的按鈕。您的代碼是在您調用pressBtn:操作時創建了一個新按鈕。由於這個新按鈕的tag值爲0,因此沒有任何發生。我建議的代碼實際上使用了用戶點擊的按鈕。

+0

這將有很大幫助:D ... +1 – Till

0

使用調用者(發件人),這是您對其執行操作的項目。不要創建一個新的按鈕,你想檢查按鈕的標籤,所以新的按鈕不是你的工作項目。

試試你最好的男人,:)。