Iphone之UITableView

来源:互联网 发布:计算机视觉python 编辑:程序博客网 时间:2024/06/05 00:14

 

UITableView应该是在开发中运用最多的控件之一。所以必须很熟悉。

UITableView的定义和初始化:

[html] view plaincopy
  1. UITableView *table = [[UITableView alloc] initWithFrame:HC_SystemTableViewFrame  
  2.                                                       style:UITableViewStyleGrouped];  

和一般控件似乎没有什么却别。

初始化控件后就是将UITableView画成自己想要的效果

[html] view plaincopy
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.  static NSString *Cell = @"CELL";  
  4.  UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell];  
  5.  if (cell == nil)  
  6.  {  
  7.   cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault   
  8.             reuseIdentifier:Cell] autorelease];  
  9.  }  
  10.  NSArray *array = [NSArray arrayWithObjects:@"振动", @"声音", @"消息提醒设置",@"主题", nil];  
  11.  NSArray *arrayList = [dataList objectForKey:[array objectAtIndex:indexPath.section]];  
  12.  cell.tag = indexPath.section;  
  13.  switch (indexPath.section) {  
  14.   case 0:  
  15.   {  
  16.    cell.textLabel.text = [arrayList objectAtIndex:indexPath.row];  
  17.    cell.textLabel.textAlignment = UITextAlignmentLeft;  
  18.    cell.textLabel.font = [UIFont boldSystemFontOfSize:20];  
  19.    UISwitch *swit = [[UISwitch alloc] initWithFrame:CGRectMake(180, 8, 70, 40)];  
  20.    NSInteger switStatus = [[[MessageCenter shareInstance].settingDic objectForKey:@"setVibrate"] integerValue];  
  21.    swit.on = switStatus;  
  22.    [swit addTarget:self action:@selector(addVibrate:) forControlEvents:UIControlEventValueChanged];  
  23.    [cell.contentView addSubview:swit];  
  24.    [swit  release];  
  25.    cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  26.    break;  
  27.   }  
  28.   case 1:  
  29.   {  
  30.    cell.accessoryType = UITableViewCellAccessoryNone;  
  31.    cell.textLabel.text = [arrayList objectAtIndex:indexPath.row];  
  32.    cell.textLabel.textAlignment = UITextAlignmentLeft;  
  33.    cell.textLabel.font = [UIFont boldSystemFontOfSize:20];  
  34.    UISwitch *swit = [[UISwitch alloc] initWithFrame:CGRectMake(180, 8, 70, 40)];  
  35.    NSInteger switStatus1 = [[[MessageCenter shareInstance].settingDic objectForKey:@"setSound"] integerValue];  
  36.    swit.on = switStatus1;  
  37.    [swit addTarget:self action:@selector(addSound:) forControlEvents:UIControlEventValueChanged];  
  38.    [cell.contentView addSubview:swit];  
  39.    [swit  release];  
  40.    cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  41.    break;  
  42.   }  
  43.   case 2:  
  44.   {  
  45.    cell.accessoryType = UITableViewCellAccessoryNone;  
  46.    cell.textLabel.text = [arrayList objectAtIndex:indexPath.row];  
  47.    cell.textLabel.textAlignment = UITextAlignmentLeft;  
  48.    cell.textLabel.font = [UIFont boldSystemFontOfSize:20];  
  49.    UISwitch *swit = [[UISwitch alloc] initWithFrame:CGRectMake(180, 8, 70, 40)];  
  50.    NSInteger switStatus2 = [[[MessageCenter shareInstance].settingDic objectForKey:@"setNotification"] integerValue];  
  51.    swit.on = switStatus2;  
  52.    [swit addTarget:self action:@selector(addNotification:) forControlEvents:UIControlEventValueChanged];  
  53.    [cell.contentView addSubview:swit];  
  54.    [swit  release];  
  55.    cell.selectionStyle = UITableViewCellSelectionStyleNone;  
  56.    break;  
  57.   }  
  58.   case 3:  
  59.   {  
  60.    cell.textLabel.text = [arrayList objectAtIndex:indexPath.row];  
  61.    cell.textLabel.textAlignment = UITextAlignmentCenter;  
  62.    cell.textLabel.font = [UIFont boldSystemFontOfSize:20];  
  63.    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;  
  64.    MessageCenter *mgCenter = [MessageCenter shareInstance];  
  65.      
  66.    UILabel *lable = (UILabel *)[cell.contentView viewWithTag:101];  
  67.    if (lable != 0)  
  68.     [lable removeFromSuperview];  
  69.    lable = [[UILabel alloc] initWithFrame:CGRectMake(200, 14, 70, 20)];  
  70.    lable.backgroundColor = [UIColor clearColor];  
  71.    lable.text = @"";  
  72.    lable.tag = 101;  
  73.    lable.textAlignment = UITextAlignmentRight;  
  74.    lable.font = [UIFont systemFontOfSize:13];  
  75.    if (mgCenter.themeSetName != nil)  
  76.     lable.text = mgCenter.themeSetName;  
  77.    else  
  78.     lable.text = @"默认";  
  79.    [cell.contentView addSubview:lable];  
  80.    [lable release];  
  81.    break;  
  82.   }  
  83.   default:  
  84.    break;  
  85.  }  
  86.   
  87.  return cell;  
[html] view plaincopy
  1. }  
[html] view plaincopy
  1. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section  
  2. {  
  3.  UIView *custom = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 38)] autorelease];   
  4.  UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 38)];  
  5.  label.backgroundColor = [UIColor clearColor];  
  6.  NSArray *array = [NSArray arrayWithObjects:@"振动", @"声音",@"消息提醒设置",@"主题", nil];  
  7.  label.text = [array objectAtIndex:section];  
  8.  label.textAlignment = UITextAlignmentLeft;  
  9.  label.textColor = [UIColor whiteColor];  
  10.  label.font = [UIFont boldSystemFontOfSize:18];  
  11.  [custom addSubview:label];  
  12.  [label release];  
  13.  return custom;  
  14. }  
[html] view plaincopy
  1.    


我定义四个单元将其填充到UITableView中。

当然我将UITableViewCell 实现以后,肯定希望在点击UITableViewCell 会处理什么事件

[html] view plaincopy
  1. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];  
  4.     switch (cell.tag)   
  5.     {  
  6.         case 1:  
  7.         {  
  8.               
  9.       
  10.             break;  
  11.         }         
  12.         case 2:  
  13.         {  
  14.               
  15.             break;    
  16.         }  
  17.         case 3:  
  18.         {         
  19.             if (themeControl == nil)  
  20.             {  
  21.                 themeControl = [[ThemeViewController alloc] init];  
  22.             }  
  23.             [self.navigationController pushViewController:themeControl animated:YES];  
  24.             break;  
  25.         }  
  26.         default:  
  27.                 break;  
  28.     }  
  29. }  

原创粉丝点击