hotfix lua語(yǔ)法
-- 替換WSFRefundBaseCell類里相關(guān)的方法
interface {"WSFRefundBaseCell"}
-- 替換實(shí)例方法
function initSubViews(self)
print("begin------ WSFRefundBaseCell replace function initSubViews ------")
-- self.super:initSubViews(self)
-- 1庐船、ORIG:調(diào)用原有initSubViews方法
self:ORIGinitSubViews()
-- 創(chuàng)建UI + 布局
local button = UIButton:buttonWithType(UIButtonTypeCustom)
button:setTitle_forState("LuaButton", UIControlStateNormal)
button:setTitleColor_forState(UIColor:blueColor(), UIControlStateNormal)
button:setBackgroundColor(UIColor:lightGrayColor())
self:containerView():addSubview(button)
-- 2银酬、帶下劃線的方法:UNDERxLINE 代替 _
toobjc(button):masUNDERxLINEmakeConstraints(
toblock(
function(make)
make:top():equalTo()(self:containerView()):offset()(40)
make:right():equalTo()(self:containerView()):inset()(20)
-- make:top():masUNDERxLINEequalTo(self:containerView())
-- make:right():masUNDERxLINEequalTo(self:containerView())
end
,{"void", "MASConstraintMaker *"}
)
)
-- 設(shè)置Label顏色
local refundContentViewLeftLabel = self:refundContentView():refundMoneyLabel():leftLabel()
refundContentViewLeftLabel:setTextColor(UIColor:blueColor())
-- 原有UI 更新布局
local rightLabel = self:refundContentView():titleView():rightLabel()
rightLabel:setTextColor(UIColor:blueColor())
local leftLabel = self:refundContentView():titleView():leftLabel()
toobjc(rightLabel):masUNDERxLINEremakeConstraints(
toblock(
function(make)
make:leading():equalTo()(leftLabel:masUNDERxLINEtrailing()):offset()(20)
make:centerY():equalTo()(leftLabel)
end
,{"void", "MASConstraintMaker *"}
)
)
print("end------ WSFRefundBaseCell replace function initSubViews ------")
end
-- 替換實(shí)例方法
function setRefundModel(self, refundModel)
self:ORIGsetRefundModel(refundModel)
local applyPrice = string.format("Lua實(shí)際退款金額:%s",refundModel:realPrice())
self:refundContentView():refundMoneyLabel():leftLabel():setText(applyPrice)
end
- WSFRefundCellContentView類里新增屬性、方法
-- WSFRefundCellContentView類里新增屬性筐钟、方法
interface {"WSFRefundCellContentView", protocols = {"UITableViewDelegate", "UITableViewDataSource"}}
-- 新增 luaTableView 屬性
function luaTableView(self)
if self._luaTableView == nil then
self._luaTableView = UITableView:init()
self._luaTableView:setDelegate(self)
self._luaTableView:setDataSource(self)
self._luaTableView:setBackgroundColor(UIColor:lightGrayColor())
end
return self._luaTableView
end
function setLuaTableView(self, luaTableView)
self._luaTableView = luaTableView
end
-- 新增方法(返回值和參數(shù)均為id類型)
function initLuaSubViews(self)
self.models = LuaTableViewModel:getLuaTableViewModels()
local view = toobjc(self:luaTableView())
local orderNumberLabel = self:orderNumberLabel()
self:addSubview(view)
-- view:setFrame(CGRect(10,10,300,200))
print("打印orderNumberLabel = " .. tostring(orderNumberLabel))
toobjc(view):masUNDERxLINEmakeConstraints(
toblock(
function(make)
make:leading():equalTo()(orderNumberLabel:leftLabel():masUNDERxLINEtrailing())
make:trailing():equalTo()(self)
make:top():equalTo()(self)
make:bottom():equalTo()(self)
end
,{"void", "MASConstraintMaker *"}
)
)
print("打印view = " .. tostring(view))
return nil
end
function tableView_numberOfRowsInSection(self, table, section)
local count = toobjc(self.models):count()
return count
end
function tableView_cellForRowAtIndexPath(self, table, indexPath)
local cell = table:dequeueReusableCellWithIdentifier("cellID")
if cell == nil then
cell = UITableViewCell:initWithStyle_reuseIdentifier(UITableViewCellStyleDefault, "cellID")
end
local text = string.format("第%d個(gè)cell", indexPath:row())
cell:textLabel():setText(text)
local model = self.models[indexPath:row() + 1]
cell:textLabel():setText(model:title())
print("model = " .. tostring(model:title()))
return cell
end
-- 替換initSubViews方法
function initSubViews(self)
self:ORIGinitSubViews()
self:initLuaSubViews(self)
end
-- 新增LuaTableViewModel類
interface {"LuaTableViewModel", NSObject}
-- 新增 title 屬性
function title(self)
return self._title
end
function setTitle(self, title)
self._title = title
end
-- 替換init方法
function init(self)
self.super:init()
return self
end
-- 新增類方法(返回值和參數(shù)均為id類型)
function LuaTableViewModel:getLuaTableViewModels(self)
-- 其中, start是起始值, limit是結(jié)束值, step是步進(jìn)(可省, 默認(rèn)是1).
-- i是for循環(huán)的local變量, for循環(huán)之后i不存在.
local arrays = {}
for i = 10, 1, -1 do
local model = LuaTableViewModel:init()
local title = string.format("i = %s", tostring(i))
model:setTitle(title)
table.insert(arrays, model)
end
-- for i, v in pairs(arrays) do
-- print("vtitle = " .. tostring(v:title()))
-- end
return arrays
end
hotfix lua語(yǔ)法
-- OC CGFloat width = [UIScreen mainScreen].bounds.size.width;
-- local width = UIScreen:mainScreen():bounds().width;
-- Lua對(duì)象轉(zhuǎn)換為OC對(duì)象:toobjc
-- local testString = "Hello lua!";
-- local bigFont = UIFont:boldSystemFontOfSize(30);
-- local size = toobjc(testString):sizeWithFont(bigFont);
-- puts(size);
-- Lua或者OC對(duì)象轉(zhuǎn)換為字符串:tostring
-- local num = 123;
-- local str = tostring(num);
-- print(tostring(xxx));
-- 調(diào)用類方法
-- [UT commitEvent:123 arg1:@"456" arg2:@"789"];
-- UT:commitEvent_arg1_arg2("123", "456", "789");
-- 替換類方法
-- function commitEvent_arg1_arg2(self, event, arg1, arg2);
-- self:ORIGcommitEvent_arg1_arg2(event, arg1, arg2);
-- 字典/數(shù)組
-- toobjc(self:muteDict()):objectForKey("k1")
-- toobjc(self:muteArray()):objectAtIndex(1)
-- toobjc(self:muteDict()):setObject_forKey("v11", "k1")
-- print(self:muteDict()["k1"])
-- 新增方法(返回值和參數(shù)均為id類型)
-- function myLostMethod(self)
-- print("myLostMethod")
-- return nil
-- end
-- function myLostMethod_other(self, a, b)
-- print("myLostMethod_other");
-- return nil
-- end
-- 新增屬性
-- 方式1:直接使用lua自帶的新增屬性功能揩瞪,但是只能被lua使用
-- self.xx = "abcd"
-- print(self.xx)
-- 方式2:模擬OC的增加屬性功能,可以在OC訪問(wèn)
-- function xxx(self)
-- return self._xxx
-- end
-- function setXxx(self, xxx)
-- self._xxx = xxx
-- end
-- 正確使用Lua字符串(Lua string不能直接調(diào)用NSString方法篓冲,否則會(huì)報(bào)nil錯(cuò)誤)
-- local x = string.len(str)
-- if not tempDeviceID == self:deviceID() then
-- end
-- string1 = "Lua"
-- string2 = "Tutorial"
-- local s = string1 .. " abc " .. string2 --拼接
-- print(string.format("基本格式化 %s %s",string1,string2))
-- 將Lua字符串轉(zhuǎn)成 OC NSString 對(duì)象李破,再調(diào)用NSString的OC方法
-- local x = toobjc(str):length()
-- if not toobjc(tempDeviceID):isEqualToString(self:deviceID()) then
-- end
-- NSString *x = NSStringFromClass(“TestVC”)用local x = self:class():description() 代替
-- 日期格式化
-- date = 2; month = 1; year = 2014
-- print(string.format("日期格式化 %02d/%02d/%03d", date, month, year))
-- 數(shù)組(下標(biāo)從1開始!)
-- local array = {"a", "b", "c"}
-- table.insert(array, "d") --末尾添加
-- table.insert(array, 2, "e") --第二個(gè)位置插入
-- for i = 1, #array do
-- print(array[i])
-- end
-- for i, v in pairs(array) do --i為下標(biāo)
-- print(v)
-- end
-- 字典
-- local dict = {k1="v1", k2="v2"}
-- print(dict["k1"])
-- print(dict.k1)
-- 獲取到的對(duì)象已轉(zhuǎn)為table壹将,而table沒有這些方法嗤攻,想要使用OC的方法可以先通過(guò)toobjc
-- toobjc(self:muteDict()):objectForKey("k1")
-- toobjc(self:muteArray()):objectAtIndex(1)
-- toobjc(self:muteDict()):setObject_forKey("v11", "k1")
-- print(self:muteDict()["k1"])
hotfix lua語(yǔ)法參考文檔
lua語(yǔ)法常見報(bào)錯(cuò) attempt to index local 'make' (a nil value)
-- 在lua中調(diào)用方法一定要用冒號(hào)“:”,不然會(huì) attempt to index local 'make' (a nil value)