React Native TableViewCell封装
来源:互联网 发布:淘宝专业版免费的模板 编辑:程序博客网 时间:2024/06/04 18:54
iOS上TableViewCell在开发中很是常见,所以对cell的封装很有必要。
import React, { Component } from 'react';import { StyleSheet, View, Image, Dimensions, TouchableNativeFeedback, TouchableOpacity, Platform, Text} from 'react-native';const { width, height } = Dimensions.get('window');import ARR_ICON from '../img/next.png';const MARGIN_WIDTH = 10;//模块声名并导出export default class TableCell extends Component { //属性声名 static propTypes = { onClick: React.PropTypes.func, title: React.PropTypes.string.isRequired, icon: React.PropTypes.element, extra: React.PropTypes.any, extraTextStyle: React.PropTypes.any, cellStyle: React.PropTypes.any, underLine: React.PropTypes.number, cellHeight: React.PropTypes.number, rightIcon: React.PropTypes.element, showArrow: React.PropTypes.bool, enbleClick: React.PropTypes.bool }; //默认属性 static defaultProps = { underLine: 0, cellStyle: null, extra: null, extraTextStyle: null, title: '标题', icon: null, onClick: () => { }, cellHeight: 44, showArrow: true, enbleClick: true }; //构造函数 constructor(props) { super(props); this.state = { //状态机变量声明 } } //渲染 render() { const Touch = Platform.OS === 'android' ? TouchableNativeFeedback : TouchableOpacity; let cellHeight = this.props.cellHeight; cellHeight = Constant.scale(this.props.cellHeight) let LINE_STYLE = styles.lineAll; switch (this.props.underLine) { case 1: LINE_STYLE = styles.lineMargin; break; case 2: LINE_STYLE = styles.lineNull; break; default: break; } return ( <Touch {...this.props} activeOpacity={this.props.enbleClick ? 0.2 : 1} onPress={this.props.onClick} > <View style={[{ width: width, height: cellHeight, paddingLeft: MARGIN_WIDTH, paddingRight: MARGIN_WIDTH, backgroundColor: 'white', flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', }, this.props.cellStyle ]}> <View style={styles.titleView}> {this.props.icon && <View style={{ marginRight: 5 }}>{this.props.icon}</View>} <Text style={styles.titleText}>{this.props.title}</Text> </View> <View style={styles.titleView}> {this.props.extra && typeof this.props.extra ==='string'?<Text style={[styles.extraText, this.props.extraTextStyle]}>{this.props.extra}</Text>:this.props.extra} {this.props.rightIcon ? this.props.rightIcon : this.props.showArrow ? <Image source={ARR_ICON} /> : null } </View> <View style={LINE_STYLE} /> </View> </Touch> ) }};const styles = StyleSheet.create({ listItemContainer: { flexDirection: 'row', alignItems: 'center', }, listItemIcon: { marginRight: MARGIN_WIDTH, width: 26, height: 26, }, listItemTitle: { color: 'black' }, titleText: { fontSize: 14, color: "#333333" }, extraText: { fontSize: 14, color: "#777777", marginRight: 5 }, titleView: { flexDirection: 'row', alignItems: 'center', justifyContent: 'center', }, lineAll: { width: width, height: StyleSheet.hairlineWidth, backgroundColor: '#ddd8d8', position: 'absolute', bottom: StyleSheet.hairlineWidth, left: 0 }, lineMargin: { width: width, height: StyleSheet.hairlineWidth, backgroundColor: '#ddd8d8', position: 'absolute', bottom: StyleSheet.hairlineWidth, left: MARGIN_WIDTH }, lineNull: { width: width, height: 0, position: 'absolute', bottom: 0, left: 0 }});
使用:
<TableCell underLine={1} // 0下划线全通, 1下划线有left间距 2没有下划线 cellStyle={{ backgroundColor: 'white',marginTop:64 }} // cell的样式 title="设置" // 左边title extra={"点击查看"} // 右边title onClick={() => { }} // cell点击事件 enbleClick={true} // 是否能够点击,默认为true showArrow={false} // 展示箭头, 默认为true, 如果为rightIcon有值,默认不展示箭头 rightIcon={<Image style={{ width: 20, height: 20, }} // 右边icon 默认为箭头 source={require('../img/mine/ic_bind_qq.png')} />} icon={<Image style={{ width: 20, height: 20, }} // 左边icon source={require('../img/mine/ic_bind_qq.png')}/>} />
效果图:
阅读全文
0 0
- React Native TableViewCell封装
- react native封装UI
- react native Toast封装
- react-native http封装
- react-native modal封装
- React Native 控件封装
- react-native 组件封装示例
- react-native--万能Button封装
- react native 封装Touchable 组件
- react native Back 按钮封装
- react native 密码控件 封装
- React Native 富文本封装
- react-native组件封装与传值
- React Native 原生模块封装、发布、调用
- 将友盟分享封装为React Native组件
- React Native 封装原生UI组件(iOS)
- React Native封装原生UI组件
- React-Native 自封装原生模块
- Understanding the JVM(八)垃圾收集器
- 判断线段与圆是否相交
- android webView 的WebSettings说明
- IntentService简单使用与总结
- 有关iOS11和iPhoneX的适配问题
- React Native TableViewCell封装
- Nginx学习(2)
- 周六的交流17.10.28_网络安全
- input输入手机号每四位数增加个空格
- CPDA学员请注意:老师们又在研究课程新内容啦!
- spring AspectJ的Execution表达式
- 字符串常量池(String Constant Pool)深入解析
- 图像分割之(二)Graph Cut(图割)
- 创建用户故事地图的步骤