iOS经验4:自定义TableViewCell应用代码例子过程 时间戳
来源:互联网 发布:微商货源源码 编辑:程序博客网 时间:2024/04/30 08:08
不用,镔哥多说了,TAbleView在工程项目应用得应该是最多的,但是系统自带的不能满足我的需求,所以在做项目的时候一般都要自定义UITableViewCell来实现我们的目的:
下面首先我把自己的项目的一个代码做为例子介绍:
//
// RecordTableViewCell.h
// 自定义一个商品历史纪录
// Created by bin on 14/10/31.
// Copyright (c) 2014年 mac. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface RecordTableViewCell : UITableViewCell
@property (nonatomic,strong) UILabel * timeLable;//时间
@property (nonatomic,strong) UILabel * IDLabel;//ID号
@property (nonatomic,strong) UILabel * numberLabel;//购买数量
- (void)getDataByDictionary:(NSDictionary *)dic;
@end
// RecordTableViewCell.m
//
// Created by bin on 14/10/31.
// Copyright (c) 2014年 mac. All rights reserved.
//
#import "RecordTableViewCell.h"
#define BACKCOLOR [UIColor cyanColor]
@implementation RecordTableViewCell
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [superinitWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
self.timeLable = [[UILabelalloc] initWithFrame:CGRectMake(10,10, 120, 20)];
_timeLable.font = [UIFontsystemFontOfSize:12];
_timeLable.backgroundColor =BACKCOLOR;
[self addSubview:_timeLable];
self.IDLabel = [[UILabelalloc] initWithFrame:CGRectMake(_timeLable.right,_timeLable.top,80, _timeLable.height)];
_IDLabel.backgroundColor =BACKCOLOR;
_IDLabel.textAlignment =NSTextAlignmentCenter;
[self addSubview:_IDLabel];
self.numberLabel = [[UILabelalloc] initWithFrame:CGRectMake(_IDLabel.right,_IDLabel.top,110, _IDLabel.height)];
_numberLabel.backgroundColor =BACKCOLOR;
_numberLabel.font = [UIFontsystemFontOfSize:14];
_numberLabel.tintColor = [UIColorcolorWithRed:165green:3blue:16alpha:1];
[self addSubview:_numberLabel];
}
return self;
}
//购买的时间
- (void)getDataByDictionary:(NSDictionary *)dic
{
self.timeLable.text = [self getTImeBytimestampString:[dic objectForKey:@"joinTime"]];
self.IDLabel.text = [dicobjectForKey:@"joinerPhone"];
self.numberLabel.text = [NSStringstringWithFormat:@"购买了%@人次", [dic objectForKey:@"joinCount"]];
}
//根据时间戳返回时间
- (NSString *)getTImeBytimestampString:(NSString *)timstampStr
{
NSDate * date = [NSDatedateWithTimeIntervalSince1970:[timstampStr longLongValue] / 1000];
NSDateFormatter * formatter = [[NSDateFormatteralloc] init];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString * str = [NSStringstringWithFormat:@"%@", [formatterstringFromDate:date]];
return str;
}
//===========================================使用===============
在应用的窗口中就直接这样写就可以了
#pragma mark - UITabelViewDatasource
- (RecordTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
RecordTableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:identifierforIndexPath:indexPath];
NSDictionary * dic = [_dataArrayobjectAtIndex:indexPath.row];
[cell getDataByDictionary:dic];
return cell;
}
// 返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
}
cell.accessoryType = UITableViewCellAccessory
@property(nonatomic) UITableViewCellAccessory
@property(nonatomic,retain) UIView
@property(nonatomic) UITableViewCellAccessory
@property(nonatomic,retain) UIView
- (void) performExpand:(id)paramSender{
}
通过,上面一步,我们为Cell添加了一个自定义的按钮。
也许就会遇到这么一个纠结的情况,当点击UITableViewCell高亮时,其子视图中不该高亮的对象(比如说自定义的那个按钮)也高亮了。
比如:
正确方式:我们需要cell被选中时,按钮不应该也被高亮显示。如:
错误方式:但是,cell被选中时,按钮却也高亮显示了。如:
- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated{
}
因为当UITableViewCell为选中状态时,UITableViewCell把selectedBackgroundView当作一个子视图来添加;
selectedBackgroundView被添加在UITableViewCell的backgroundView之上,或者所有其它视图之下。
当调用setSelected: animated:这一方法时,会导致selectedBackgroundView以一个alpha消化的状态来出现和消失。
还应该注意:
UITableViewCell的selectionStyle值为UITableViewCellSelection
// 设置背景
[self setBackgroundImageByName
[self setBackgroundImage:[UIImage imageNamed:@"table_live_bg.png"]];
[self.contentView insertSubview:messageBackgroundViewbelowSubview:self.textLabel];
//定制Delete字符串,添加函数 返回要显示的字符串
-(NSString *)tableView:(UITableView*)tableView titleForDeleteConfirmati
}
- iOS经验4:自定义TableViewCell应用代码例子过程 时间戳
- iOS 代码实现自定义TableView 自定义TableViewCell
- IOS:最简单一个使用自定义的TableViewCell例子
- ios 自定义tableviewcell
- iOS 自定义tableViewcell
- ios 自定义tableViewcell,UITableViewCell
- iOS 自定义TableViewCell封装
- iOS TableViewCell自定义分割线
- iOS TableViewCell 自定义选中样式
- iOS 代码tableViewCell自适应label
- iOS开发:iOS TableViewCell自定义分割线
- iOS开发:iOS TableViewCell自定义分割线
- 自定义xib TableViewCell 标准写法 代码段
- 如何 iOS 编程中使用自定义 TableViewCell
- iOS自定义TableViewCell详解[两种方法]
- iOS自定义TableViewCell详解[两种方法]
- iOS自定义TableViewCell详解[两种方法]
- iOS自定义TableViewCell详解[两种步骤]
- maven 配置篇 之pom.xml
- 第十三周项目五:统计每一个字母的个数
- PHP+jQuery模拟一个进程,动态获取长时间运行的进程
- iOS RATreeView 问题总结
- php 获取今天时间戳 时间戳转为时间
- iOS经验4:自定义TableViewCell应用代码例子过程 时间戳
- Linux 安装mysql
- [iOS-GQiang]IOS下利用ziparchive解压缩zip文件
- 四则运算表达式求值(栈的应用)
- iOS8新特性Extension之Today插件的代码测试
- Delvik vm介绍
- 关于多线程那些事儿
- Ubuntu下的硬盘“分区”
- oracle的exp导出