Iphone之UITableView

效果圖如下:

 

UITableView應該是在開發中運用最多的控件之一。所以必須很熟悉。

UITableView的定義和初始化:

UITableView *table = [[UITableView alloc] initWithFrame:HC_SystemTableViewFrame
													  style:UITableViewStyleGrouped];

和一般控件似乎沒有什麼卻別。

初始化控件後就是將UITableView畫成自己想要的效果

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
 static NSString *Cell = @"CELL";
 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Cell];
 if (cell == nil)
 {
  cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
            reuseIdentifier:Cell] autorelease];
 }
 NSArray *array = [NSArray arrayWithObjects:@"振動", @"聲音", @"消息提醒設置",@"主題", nil];
 NSArray *arrayList = [dataList objectForKey:[array objectAtIndex:indexPath.section]];
 cell.tag = indexPath.section;
 switch (indexPath.section) {
  case 0:
  {
   cell.textLabel.text = [arrayList objectAtIndex:indexPath.row];
   cell.textLabel.textAlignment = UITextAlignmentLeft;
   cell.textLabel.font = [UIFont boldSystemFontOfSize:20];
   UISwitch *swit = [[UISwitch alloc] initWithFrame:CGRectMake(180, 8, 70, 40)];
   NSInteger switStatus = [[[MessageCenter shareInstance].settingDic objectForKey:@"setVibrate"] integerValue];
   swit.on = switStatus;
   [swit addTarget:self action:@selector(addVibrate:) forControlEvents:UIControlEventValueChanged];
   [cell.contentView addSubview:swit];
   [swit  release];
   cell.selectionStyle = UITableViewCellSelectionStyleNone;
   break;
  }
  case 1:
  {
   cell.accessoryType = UITableViewCellAccessoryNone;
   cell.textLabel.text = [arrayList objectAtIndex:indexPath.row];
   cell.textLabel.textAlignment = UITextAlignmentLeft;
   cell.textLabel.font = [UIFont boldSystemFontOfSize:20];
   UISwitch *swit = [[UISwitch alloc] initWithFrame:CGRectMake(180, 8, 70, 40)];
   NSInteger switStatus1 = [[[MessageCenter shareInstance].settingDic objectForKey:@"setSound"] integerValue];
   swit.on = switStatus1;
   [swit addTarget:self action:@selector(addSound:) forControlEvents:UIControlEventValueChanged];
   [cell.contentView addSubview:swit];
   [swit  release];
   cell.selectionStyle = UITableViewCellSelectionStyleNone;
   break;
  }
  case 2:
  {
   cell.accessoryType = UITableViewCellAccessoryNone;
   cell.textLabel.text = [arrayList objectAtIndex:indexPath.row];
   cell.textLabel.textAlignment = UITextAlignmentLeft;
   cell.textLabel.font = [UIFont boldSystemFontOfSize:20];
   UISwitch *swit = [[UISwitch alloc] initWithFrame:CGRectMake(180, 8, 70, 40)];
   NSInteger switStatus2 = [[[MessageCenter shareInstance].settingDic objectForKey:@"setNotification"] integerValue];
   swit.on = switStatus2;
   [swit addTarget:self action:@selector(addNotification:) forControlEvents:UIControlEventValueChanged];
   [cell.contentView addSubview:swit];
   [swit  release];
   cell.selectionStyle = UITableViewCellSelectionStyleNone;
   break;
  }
  case 3:
  {
   cell.textLabel.text = [arrayList objectAtIndex:indexPath.row];
   cell.textLabel.textAlignment = UITextAlignmentCenter;
   cell.textLabel.font = [UIFont boldSystemFontOfSize:20];
   cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
   MessageCenter *mgCenter = [MessageCenter shareInstance];
   
   UILabel *lable = (UILabel *)[cell.contentView viewWithTag:101];
   if (lable != 0)
    [lable removeFromSuperview];
   lable = [[UILabel alloc] initWithFrame:CGRectMake(200, 14, 70, 20)];
   lable.backgroundColor = [UIColor clearColor];
   lable.text = @"";
   lable.tag = 101;
   lable.textAlignment = UITextAlignmentRight;
   lable.font = [UIFont systemFontOfSize:13];
   if (mgCenter.themeSetName != nil)
    lable.text = mgCenter.themeSetName;
   else
    lable.text = @"默認";
   [cell.contentView addSubview:lable];
   [lable release];
   break;
  }
  default:
   break;
 }

 return cell;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
 UIView *custom = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 38)] autorelease]; 
 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 150, 38)];
 label.backgroundColor = [UIColor clearColor];
 NSArray *array = [NSArray arrayWithObjects:@"振動", @"聲音",@"消息提醒設置",@"主題", nil];
 label.text = [array objectAtIndex:section];
 label.textAlignment = UITextAlignmentLeft;
 label.textColor = [UIColor whiteColor];
 label.font = [UIFont boldSystemFontOfSize:18];
 [custom addSubview:label];
 [label release];
 return custom;
}
 


我定義四個單元將其填充到UITableView中。

當然我將UITableViewCell 實現以後,肯定希望在點擊UITableViewCell 會處理什麼事件

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
	switch (cell.tag) 
	{
		case 1:
		{
			
	
			break;
		}		
		case 2:
		{
			
			break;	
		}
		case 3:
		{		
			if (themeControl == nil)
			{
				themeControl = [[ThemeViewController alloc] init];
			}
			[self.navigationController pushViewController:themeControl animated:YES];
			break;
		}
		default:
		        break;
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章