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;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值