gpt4 book ai didi

ios - 使按钮在按下时填充 UITableView

转载 作者:行者123 更新时间:2023-11-29 13:25:31 25 4
gpt4 key购买 nike

我创建了一个简单的 rss 阅读器,可以毫无问题地填充 viewdidLoad 上的 tableviewcell。我的问题是每当我单击我的按钮时,tableview 都不会改变。我尝试用里面的按钮修饰重新加载,但仍然没有任何反应。 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

最佳答案

在您的 pressBtn: 方法中,替换此行:

UIButton *button= [UIButton buttonWithType:UIButtonTypeRoundedRect];

与:

UIButton *button = (UIButton *)sender;

这假设在 IB 中您已经将 pressBtn: 方法连接到适当的按钮。当您的 pressBtn: 操作被调用时,您拥有的代码正在创建一个新按钮。由于这个新按钮的 tag 值为 0,因此什么也没有发生。我建议的代码实际上使用了用户点击的按钮。

关于ios - 使按钮在按下时填充 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13241024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com