我创建了一个简单的rss阅读器,可以在viewdidLoad上填充tableviewcell而不会出现问题。我的问题是每当我点击我的按钮时,tableview不会改变。我尝试重新加载里面的按钮touchup并没有任何事情发生。相当新的Xcode和iOS编程,所以任何帮助都很棒。
.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
答案 0 :(得分:1)
在pressBtn:
方法中,替换此行:
UIButton *button= [UIButton buttonWithType:UIButtonTypeRoundedRect];
使用:
UIButton *button = (UIButton *)sender;
这假设您已在IB中将pressBtn:
方法连接到相应的按钮。您的代码是在调用pressBtn:
操作时创建新按钮。由于此新按钮的tag
值为0,因此没有任何结果。我建议的代码实际上使用了用户点击的按钮。
答案 1 :(得分:0)
使用调用者(发件人),即您对其执行操作的项目。不要创建新按钮,要检查按钮的标签,以便新按钮不是您工作的项目。
试试你最好的男人,:)。