FMHomeViewController.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //
  2. // FMHomeViewController.m
  3. // FreeMusic
  4. //
  5. //
  6. #import "FMHomeViewController.h"
  7. #import "FMMusicViewController.h"
  8. #import "ShouCangVC.h"
  9. #import "SearchViewController.h"
  10. #import "DBDaoHelper.h"
  11. @interface FMHomeViewController ()
  12. {
  13. NSArray * array;
  14. }
  15. @end
  16. @implementation FMHomeViewController
  17. - (void)viewDidLoad
  18. {
  19. [super viewDidLoad];
  20. // self.view.backgroundColor = [UIColor redColor];
  21. self.navigation.title = @"分类";
  22. [self.navigationController setNavigationBarHidden:YES animated:NO];
  23. self.view.backgroundColor = [UIColor whiteColor];
  24. self.navigation.leftImage =nil;// [UIImage imageNamed:@"nav_backbtn.png"];
  25. self.navigation.rightImage = nil;
  26. // self.navigation.headerImage = [UIImage imageNamed:@"nav_canadaicon.png"];
  27. self.navigation.navigaionBackColor = [UIColor colorWithRed:192.0f/255.0f green:37.0f/255.0f blue:62.0f/255.0f alpha:1.0f];
  28. self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0f,self.navigation.size.height+self.navigation.origin.y, self.view.size.width, 44.0f)];
  29. _searchBar.delegate =self;
  30. _searchBar.placeholder = @"搜索:音乐";
  31. // _searchBar.tintColor = [UIColor lightGrayColor];
  32. _searchBar.showsCancelButton = YES;
  33. _searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
  34. _searchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;
  35. _searchBar.keyboardType = UIKeyboardTypeDefault;
  36. _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, _searchBar.size.height+_searchBar.origin.y, self.view.size.width, self.view.size.height-self.navigation.size.height-self.searchBar.size.height-48.5f)];
  37. _tableView.delegate =self;
  38. _tableView.dataSource = self;
  39. // _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  40. [self.view addSubview:_tableView];
  41. // array = [NSArray arrayWithObjects:@"欢快",@"甜蜜" ,@"安静",@"伤感",@"思念",nil];
  42. array = [DBDaoHelper myType];
  43. [self.view addSubview:_searchBar];
  44. UIButton * _buttonFriend= [UIButton buttonWithType:UIButtonTypeCustom];
  45. [_buttonFriend setTitle:@"添加分类" forState:UIControlStateNormal];
  46. [_buttonFriend setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  47. // [_buttonFriend setTitleColor:kColor(220, 151, 32, 1) forState:UIControlStateNormal];
  48. [_buttonFriend addTarget:self action:@selector(topViewClick) forControlEvents:UIControlEventTouchUpInside];
  49. _buttonFriend.titleLabel.font = [UIFont systemFontOfSize:15];
  50. _buttonFriend.frame = CGRectMake(230-30, 30, 75+30, 30);
  51. // [_buttonFriend setBackgroundColor:[UIColor greenColor]];
  52. [self.navigation addSubview:_buttonFriend];
  53. }
  54. -(void)topViewClick{
  55. UIAlertView *customAlertView = [[UIAlertView alloc] initWithTitle:@"请输入您的分类名称" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
  56. [customAlertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
  57. UITextField *nameField = [customAlertView textFieldAtIndex:0];
  58. nameField.placeholder = @"添加分类";
  59. [customAlertView show];
  60. }
  61. //弹出的代理
  62. -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
  63. if (buttonIndex == alertView.firstOtherButtonIndex) {
  64. UITextField *nameField = [alertView textFieldAtIndex:0];
  65. [DBDaoHelper insertMyTypeWith:nameField.text];
  66. array = [DBDaoHelper myType];
  67. [_tableView reloadData];
  68. //TODO
  69. }
  70. }
  71. //搜索取消按钮点击
  72. - (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
  73. {
  74. [_searchBar resignFirstResponder];
  75. }
  76. //搜索按钮点击,进入搜索我的歌曲页面
  77. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  78. {
  79. // called when keyboard search button pressed
  80. [_searchBar resignFirstResponder];
  81. SearchViewController * pushController = [[SearchViewController alloc] init];
  82. pushController.nameMusic = searchBar.text;
  83. [self.navigationController pushViewController:pushController animated:YES];
  84. }
  85. #pragma mark - Table view data source,下面都是列表的代理事件
  86. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  87. {
  88. return 1;
  89. }
  90. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  91. {
  92. return [array count];
  93. }
  94. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  95. {
  96. NSString * dentifier = @"cell";
  97. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:dentifier];
  98. if (cell == nil) {
  99. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:dentifier];
  100. }
  101. cell.textLabel.text = [array objectAtIndex:indexPath.row];
  102. return cell;
  103. }
  104. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  105. {
  106. return 44;
  107. }
  108. //列表点击代理
  109. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  110. {
  111. ShouCangVC * pushController = [[ShouCangVC alloc] init];
  112. pushController.typeMusic = indexPath.row+1;
  113. [self.navigationController pushViewController:pushController animated:YES];
  114. }
  115. //设置左滑出现删除按钮
  116. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
  117. editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath //当在Cell上滑动时会调用此函数
  118. {
  119. if(indexPath.row>=5)
  120. return UITableViewCellEditingStyleDelete; //返回此值时,Cell会做出响应显示Delete按键,点击Delete后会调用下面的函数,别给传递UITableViewCellEditingStyleDelete参数
  121. else
  122. return UITableViewCellEditingStyleNone;//返回此值时,Cell上不会出现Delete按键,即Cell不做任何响应
  123. }
  124. //删除按钮点击
  125. - (void) tableView:(UITableView *)tableView
  126. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
  127. forRowAtIndexPath:(NSIndexPath *)indexPath//对选中的Cell根据editingStyle进行操作
  128. {
  129. if (editingStyle == UITableViewCellEditingStyleDelete)
  130. {
  131. NSArray *arrTypeId = [DBDaoHelper myTypeId];
  132. NSString *type = [arrTypeId objectAtIndex:indexPath.row];
  133. [DBDaoHelper DeleteMyTypeWith:type];
  134. array = [DBDaoHelper myType];
  135. [_tableView reloadData];
  136. }
  137. }
  138. - (void)didReceiveMemoryWarning
  139. {
  140. [super didReceiveMemoryWarning];
  141. }
  142. @end