根據蘋果審核的要求,請大家暫時不要使用JSPatch了,建議已經集成了的童鞋,在下次提交的時候刪除相關內容。
RT,這篇文章講的是在使用JSPatch的過程中的注意事項,至于JSPatch的集成和基礎使用說明請移步。
下面針對在使用JSPatch過程中的一些細節做出說明,有些內容在基礎使用說明里面可以查閱到。
OC到JS代碼的轉換
- 枚舉。OC代碼里面的枚舉統一改成對應的常量。
oc
typedef NS_ENUM(NSInteger, xxx)
{
a = 1,
b
}
switch(x)
{
case a:
break;
case b:
break
}
js
switch(x) {
case 1:
break;
case 2:
break;
}
- 布爾變量。統一使用0或1代替OC中的bool變量
- 宏或靜態常量。使用字面值代替。
oc
#define CELLID @"cellid"
[tableView dequeueReusableCellWithIdentifier:CELLID];
js
[tableView dequeueReusableCellWithIdentifier:@"cellid"];
- 數組長度。在main.js中獲取數組長度
array.count()
self.array().count()
- 遍歷數組
不支持語法糖。請使用objectAtIndex
- 打印。
console.log()
下面給出一個事例
OC
static NSString *kCellID = @"cellid";
#define UIColorFromHexAndAlpha(s, a) [UIColor colorWithHexString:@#s alpha:a]
#define UIColorFromHex(s) [UIColor colorWithHexString:@#s]
typedef NS_ENUM(NSInteger, TestStatus)
{
TestStatusComplete,// 已結束
TestStatusPrepare, // 未開始
TestStatusDoing, // 進行中
};
typedef NS_ENUM(NSInteger, TestType)
{
TestTypeSelectFirst = 1,
TestTypeSecond
};
@implementation TestViewController
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TestModel *model;
if (indexPath.row < self.dataSource.count)
{
model = self.dataSource[indexPath.row];
}
TestTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellID];
if (model == nil)
{
return cell;
}
cell.titleLabel.text = model.explain;
// 直播活動不顯示選角信息
// 如果參與了選角顯示自己獲得的票數,否則顯示參與活動的關注的主播獲得的票數
if (model.type == TestTypeSelectFirst)
{
cell.firstButton.hidden = NO;
cell.bottomGrayView.hidden = NO;
if (model.name.length > 0)
{
[cell.firstButton setAttributedTitle:model.firstInfo forState:UIControlStateNormal];
}
else
{
[cell.fistButton setAttributedTitle:model.secondInfo forState:UIControlStateNormal];
}
}
else
{
cell.votesButton.hidden = YES;
cell.bottomGrayView.hidden = YES;
}
[cell.coverImageView imageWithUrlStr:model.pic placeholderImageStr:kDefaultStudio];
NSString *stateStr;
UIColor *stateBg;
UIColor *textColor = [UIColor whiteColor];
cell.stateLabel.hidden = NO;
switch (model.status)
{
case TestStatusPrepare:
{
stateStr = @"準備中";
stateBg = UIColorFromHexAndAlpha(000000, 0.4);
break;
}
case TestStatusDoing:
{
stateStr = @"進行中";
stateBg = UIColorFromHexAndAlpha(FFDE0A, 1.0);
textColor = UIColorFromHex(333333);
break;
}
case TestStatusComplete:
{
stateStr = @"已結束";
stateBg = UIColorFromHex(A0A0A0);
break;
}
default:
cell.stateLabel.hidden = YES;
break;
}
cell.stateLabel.text = stateStr;
cell.stateLabel.backgroundColor = stateBg;
cell.stateLabel.textColor = textColor;
cell.sponsorPhoto = model.photo;
cell.sponsorNickname = model.nickName;
return cell;
}
JS
require('UIColor');
defineClass('TestViewController', {
tableView_cellForRowAtIndexPath: function(tableView, indexPath) {
var model;
var row = indexPath.row();
if (row < self.dataSource().count()) {
model = self.dataSource().objectAtIndex(indexPath.row());
}
var cell = tableView.dequeueReusableCellWithIdentifier("cellid");
if (!cell) {
cell = YZJStudioTableViewCell.alloc().initWithStyle_reuseIdentifier(UITableViewCellSelectionStyleDefault, "cellid");
}
if (!model) {
return cell;
}
cell.titleLabel().setText(model.explain());
// 直播活動不顯示選角信息
// 如果參與了選角顯示自己獲得的票數,否則顯示參與活動的關注的主播獲得的票數
if (model.type() == 1) {
cell.firstButton().setHidden(0);
cell.bottomGrayView().setHidden(0);
if (model.roleName().length() > 0) {
cell.firstButton().setAttributedTitle_forState(model.firstInfo(), 0);
} else {
cell.votesButton().setAttributedTitle_forState(model.secondInfo(), 0);
}
} else {
cell.firstButton().setHidden(1);
cell.bottomGrayView().setHidden(1);
}
cell.coverImageView().imageWithUrlStr_placeholderImageStr(model.pic(), "defult_pic");
var stateStr;
var stateBg;
var textColor = UIColor.whiteColor();
cell.stateLabel().setHidden(0);
switch (model.status()) {
case 1:
{
stateStr = "準備中";
stateBg = UIColor.colorWithHexString_alpha("#000000", 0.4);;
break;
}
case 2:
{
stateStr = "進行中";
stateBg = UIColor.colorWithHexString_alpha("#FFDE0A", 1.0);
textColor = UIColor.colorWithHexString("#333333");;
break;
}
case 0:
{
stateStr = "已結束";
stateBg = UIColor.colorWithHexString("#A0A0A0");;
break;
}
default:
cell.stateLabel().setHidden(1);
break;
}
cell.stateLabel().setText(stateStr);
cell.stateLabel().setBackgroundColor(stateBg);
cell.stateLabel().setTextColor(textColor);
cell.setSponsorPhoto(model.photo());
cell.setSponsorNickname(model.nickName());
return cell;
},
});