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