您的位置:首页 > 移动开发 > IOS开发

ios 写枚举

2014-04-02 18:35 288 查看
.h文件中。。。
typedef NS_OPTIONS(NSUInteger, LoginOrReg_phone) {  //NSUInteger枚举类型,LoginOrReg_phone枚举名称
    loginPhoneType = 0, // 登录的状态
    regPhoneType = 1    // 注册的状态
};
申明一个属性

@property LoginOrReg_phone loginOrReg_phone;
.m文件中。。。用switch 进行分类

switch (_loginOrReg_phone) {
        caseloginPhoneType:
        {
            UILabel *asd = [[UILabelalloc]initWithFrame:CGRectMake(14.0f,registered.bottom+25.0f/2,SCREEN_WIDTH-28,
25)];
            [asd setBackgroundColor:[UIColorredColor]];
            [self.viewaddSubview:asd];
        }
            break;
        caseregPhoneType:
        {
            RTLabel *state = [[RTLabelalloc]      //RTLabel的使用
                              initWithFrame:CGRectMake(10.0f,registered.bottom+20.0f/2
, SCREEN_WIDTH-2, 30)];
            [self.viewaddSubview:state];
            NSString *text =@"<font color='#999999'>点击上面的 “注册”按钮,
即表示你同意“聚汇”的<font color='#0D6396'><a href='http://www.baidu.com'>服务条款</a></font>和<font color='#0D6396'><a href='http://www.sina.com'>隐私政策</a></font></font>";
            state.delegate =
self;
            [state setText:text];            
        }
            break;
        default:
            break;
    }

#pragma mark  RTLabel点击链接地址

-(void)rtLabel:(id)rtLabel didSelectLinkWithURL:(NSString *)url
{
}

在同一个tableView中使用两种风格的cell

cell.h 文件中。。。。

typedef enum  {
    CellTypeGroup, //好友分组
    CellTypeFriend, //好友
}CellType;

//-------------------------分组-----------------------
@property (strong,nonatomic)
UILabel *groupName;            //分组名称
@property (strong,nonatomic)
UILabel *groupDesc;            //分组描述
//-------------------------好友-----------------------

@property (strong,nonatomic)
SelectButton *selectButton;
@property (strong,nonatomic)
UILabel *friendName;           //好友名称
@property (strong,nonatomic)
UIImageView *stateImage;        //状态

//初始化方法
-(id)initWithCellType:(CellType)type reuseIdentifier:(NSString *)reuseIdentifier;

.m文件中。————————

-(id)initWithCellType:(CellType)type reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:reuseIdentifier]) {
  
        int cell_width = 300;
        if (IOS_DEVICE >=
7) {
            cell_width = 320;
        }
        //------好友分组单元格------
        if (type == CellTypeGroup)
        {
            //分组名称
            self.groupName = [[UILabel
alloc]initWithFrame:CGRectMake(LEFTMARGIN,
10, 0 , 0)];
            self.groupName.backgroundColor = [UIColor
clearColor];
            self.groupName.font = [UIFont
systemFontOfSize:FONT_SIZE_FTN];        
//字体大小
            [self.contentView
addSubview:self.grou
4000
pName];
            
            //分组描述
            self.groupDesc = [[UILabel
alloc]initWithFrame:CGRectMake(LEFTMARGIN,
35, 200 , 25)];
            self.groupDesc.backgroundColor = [UIColor
clearColor];
            self.groupDesc.textColor = [UIColor
grayColor];             //字体颜色
            self.groupDesc.font = [UIFont
systemFontOfSize:FONT_SIZE_TWE];        
//字体大小
            [self.contentView
addSubview:self.groupDesc];
        }
        
        //---------好友单元格---------
        else if (type ==
CellTypeFriend)
        {
            //创建选择按钮
            self.selectButton = [[SelectButton
alloc]initWithFrame:CGRectMake(260,
15, 30, 30)];
            self.selectButton.tag =
1;
            self.selectButton.hidden =
YES;
            [self.contentView
addSubview:self.selectButton];
            
            //好友状态
            self.stateImage = [[UIImageView
alloc]initWithFrame:CGRectMake(self.headImage.frame.size.width-20,
0, 20, 20)];
            [self.headImage
addSubview:self.stateImage];
            
            //好友名称
            self.friendName = [[UILabel
alloc]initWithFrame:CGRectMake(LEFTMARGIN,
10, 0 , 0)];
            self.friendName.backgroundColor = [UIColor
clearColor];
            self.friendName.font = [UIFont
systemFontOfSize:FONT_SIZE_FTN];        
//字体大小
            [self.contentView
addSubview:self.friendName];
        }
       
self.type = type;
    }
    return self;
}

在Controller.m中使用

#pragma mark-定制cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //设置cell类型
    CellType type;
    static NSString *cellIndef;
    
    if (indexPath.section ==
0) {
        type = CellTypeGroup;
        cellIndef = @"1";
    }
    if (groupArr.count ==
0) {
            type = CellTypeFriend;
            cellIndef = @"3";
    }

    //创建自定义cell
    FriendCell *cell = [tableView
dequeueReusableCellWithIdentifier:cellIndef];
    if (!cell) {
        cell = [[FriendCell
alloc]initWithCellType:type reuseIdentifier:cellIndef];
        cell.delegate = self;
    }
    //新的朋友and未分组好友
    if (indexPath.section ==
0 && type == CellTypeGroup) {
        cell.groupName.text =
name[indexPath.row];          
//获取分组名称
        [cell.groupName sizeToFit];                         
//分组名Label自适应大小
        cell.groupDesc.text =
desc[indexPath.row];          
//获取分组描述
    }
    
//好友-第二分组
    if (type == CellTypeFriend) {
        NSString *key = keyArr[indexPath.section];  
        NSArray *persons = [PersonDict
objectForKey:key];
        [cell writePersonModel:person];
    }

    return cell;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: