Guest User

Untitled

a guest
May 21st, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  NewsTitleViewController.m
  3. //  Hamshahri
  4. //
  5. //  Created by Neeku on 1/4/12.
  6. //  Copyright 2012 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9. #import "NewsTitleViewController.h"
  10. #import "RssParser.h"
  11. #import "RssData.h"
  12. #import "NewsDetailViewController.h"
  13. #import "CategoryData.h"
  14. #import "proAlertView.h"
  15.  
  16. #define MAINLABEL_TAG 1
  17. #define SECONDLABEL_TAG 2
  18. #define PHOTO_TAG 3
  19.  
  20. #pragma mark -
  21.  
  22.  
  23. @implementation NewsTitleViewController
  24.  
  25. @synthesize rssParser;
  26. @synthesize tableView;
  27. @synthesize labelText;
  28. @synthesize myCategoryData;
  29. @synthesize activityIndicator;
  30.  
  31. #pragma mark - View lifecycle
  32.  
  33. - (void)viewDidLoad
  34. {
  35.     [activityIndicator startAnimating];
  36.     [super viewDidLoad];
  37.     // Do any additional setup after loading the view from its nib.
  38.     self.title = myCategoryData.categoryName;
  39.    
  40.     rssParser = [[RssParser alloc] init];
  41.     [[self rssParser] setMyCategoryData:[self myCategoryData]];
  42.     [[self rssParser] setDelegate:self];
  43.     [[self rssParser] startProcess];
  44.    
  45.     self.view.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed:@"Background"]];
  46.     self.tableView.separatorColor = [UIColor darkGrayColor];
  47.     self.tableView.backgroundColor = [UIColor clearColor];
  48.  
  49. }
  50.  
  51. - (void)viewDidUnload
  52. {
  53.     [self setLabelText:nil];
  54.     [self setActivityIndicator:nil];
  55.     [super viewDidUnload];
  56. }
  57.  
  58. - (void) processCompleted
  59. {
  60.     [[self tableView] reloadData];
  61.     [activityIndicator stopAnimating];
  62. }
  63.  
  64. - (void) processHasErrors
  65. {
  66.     //Due to internet connection or server error.
  67.     proAlertView *alert = [[proAlertView alloc] initWithTitle:@"همشهری" message:@"دسترسی به مطالب سایت مقدور نشد. لطفاً ارتباط اینترنت خود را بررسی نمایید." delegate:self cancelButtonTitle:@"تایید" otherButtonTitles:nil];
  68.     [alert setBackgroundColor:[UIColor colorWithRed:0.1804 green: 0.3020 blue:0.1647 alpha:0.4] withStrokeColor: [UIColor colorWithHue:0.625 saturation:0.0 brightness: 0.8 alpha:0.5]];
  69.     [alert show];
  70.     [alert release];
  71.     [activityIndicator stopAnimating];
  72. }
  73.  
  74. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  75. {
  76.     return [[[self rssParser] rssItems] count];
  77. }
  78.  
  79. - (UITableViewCell *)tableView:(UITableView *)myTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  80. {
  81.     UILabel *mainLabel, *secondLabel;
  82.     UIImageView *photo;
  83.     UITableViewCell * cell = [myTableView dequeueReusableCellWithIdentifier:@"rssItemCell"];
  84.     if(nil == cell)
  85.     {
  86.         cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"rssItemCell"] autorelease];
  87.         cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  88.        
  89.         mainLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 300.0, 15.0)] autorelease];
  90.         mainLabel.tag = MAINLABEL_TAG;
  91.         mainLabel.font = [UIFont systemFontOfSize:14.0];
  92.         mainLabel.textAlignment = UITextAlignmentRight;
  93.         mainLabel.textColor = [UIColor whiteColor];
  94.         mainLabel.backgroundColor = [UIColor clearColor];
  95.         mainLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  96.         [cell.contentView addSubview:mainLabel];
  97.        
  98.         secondLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 20.0, 300.0, 25.0)] autorelease];
  99.         secondLabel.tag = MAINLABEL_TAG;
  100.         secondLabel.font = [UIFont systemFontOfSize:10.0];
  101.         secondLabel.textAlignment = UITextAlignmentRight;
  102.         secondLabel.textColor = [UIColor lightGrayColor];
  103.         secondLabel.backgroundColor = [UIColor clearColor];
  104.         secondLabel.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  105.         [cell.contentView addSubview:secondLabel];
  106.        
  107.         photo = [[[UIImageView alloc] initWithFrame:CGRectMake(225.0, 0.0, 80.0, 45.0)] autorelease];
  108.         photo.tag = PHOTO_TAG;
  109.         photo.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  110.         [cell.contentView addSubview:photo];
  111.        
  112.        
  113.         UITableViewCell * cell = [myTableView dequeueReusableCellWithIdentifier:@"rssItemCell"];
  114.         if (nil == cell)
  115.         {
  116.             cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"rssItemCell"]autorelease];
  117.             cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  118.         }
  119.        
  120.         else
  121.         {
  122.             mainLabel = (UILabel *) [cell.contentView viewWithTag:MAINLABEL_TAG];
  123.             secondLabel = (UILabel *) [cell.contentView viewWithTag:SECONDLABEL_TAG];
  124.             photo = (UIImageView *) [cell.contentView viewWithTag:PHOTO_TAG];
  125.        
  126.         }
  127.        
  128.         RssData *data = [[[self rssParser] rssItems] objectAtIndex:indexPath.row];
  129.        
  130.         mainLabel.text = [data title];
  131.         secondLabel.text = [data description];
  132.        
  133.     }
  134.    
  135.        
  136.     //To customize the selected tableview cell
  137.     //cell.selectedBackgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"effect"]]autorelease];
  138.    
  139.      [cell setSelectionStyle:UITableViewCellSelectionStyleGray];
  140.    
  141.     return cell;
  142.  
  143. }
  144.  
  145. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  146. {
  147.     NewsDetailViewController *dvcontroller = [[NewsDetailViewController alloc] initWithNibName:@"NewsDetailViewController" bundle:nil];
  148.     RssData *rssData = [[[self rssParser] rssItems] objectAtIndex:indexPath.row];
  149.    
  150.     //New Allowance
  151.     [dvcontroller setRssData:rssData];
  152.     [[self navigationController] pushViewController:dvcontroller animated:YES];
  153.     [dvcontroller release];
  154. }
  155.  
  156. // Override to allow orientations other than the default portrait orientation.
  157. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  158. {
  159.     // Return YES for supported orientations.
  160.     return YES;
  161. }
  162.  
  163.  
  164. - (void)didReceiveMemoryWarning
  165. {
  166.     // Releases the view if it doesn't have a superview.
  167.     [super didReceiveMemoryWarning];
  168.    
  169.     // Release any cached data, images, etc that aren't in use.
  170. }
  171.  
  172. - (void)viewWillAppear:(BOOL)animated
  173. {
  174.     //This UIViewController is about to re-appear, make sure we remove the current selection in our table view.
  175.     NSIndexPath *tableSelection = [self.tableView indexPathForSelectedRow];
  176.     [self.tableView deselectRowAtIndexPath:tableSelection animated:NO];
  177.    
  178. }
  179.  
  180. - (void)setTitle:(NSString *)title
  181. {
  182.     [super setTitle:title];
  183.     UILabel *titleView = (UILabel *)self.navigationItem.titleView;
  184.     if (!titleView)
  185.     {
  186.         titleView = [[UILabel alloc] initWithFrame:CGRectZero];
  187.         titleView.backgroundColor=[UIColor clearColor];
  188.         titleView.font = [UIFont boldSystemFontOfSize:20.0];
  189.         titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
  190.         titleView.textColor = [UIColor colorWithRed:0.6 green:0.6 blue:0.6 alpha:0.0];
  191.         self.navigationItem.titleView = titleView;
  192.         [titleView release];
  193.     }
  194.     titleView.text = title;
  195.     [titleView sizeToFit];
  196. }
  197.  
  198. - (void) dealloc
  199. {
  200.     [tableView release];
  201.     [rssParser release];
  202.     [myCategoryData release];
  203.     [labelText release];
  204.     [activityIndicator release];
  205.     [super dealloc];
  206. }
  207.  
  208.  
  209. @end
Add Comment
Please, Sign In to add comment