在谈及Lua中的__index,__newindex,rawget和rawset前,需要理解Lua中的元表这个概念。
零、元表的概念对Lua中元表的解释: 元表可以改变表的行为模式。
这里举个例子:
Window = {} Window.prototype = {x = 0 ,y = 0 ,width = 100 ,height = 100,} Window.mt = {} function Window.new(o) setmetatable(o ,Window.mt) return o end Window.mt.__index = Window.prototype Window.mt.__newindex = function (table ,key ,value) if key == "wangbin" then rawset(table ,"wangbin" ,"yes,i am") end end w = Window.new{x = 10 ,y = 20} w.wangbin = "55" print(w.wangbin)
然后,我们可以看到打印信息是:yes,i am
原本赋值的地方是w.wangbin = "55",但是结果却是 yes,i am。
这里就改变了元表的行为模式。
一、__index的理解__index是:当我们访问一个表中的元素不存在时,则会触发去寻找__index元方法,如果不存在,则返回nil,如果存在,则返回结果。
Window = {} Window.prototype = {x = 0 ,y = 0 ,width = 100 ,height = 100,} Window.mt = {} function Window.new(o) setmetatable(o ,Window.mt) return o end Window.mt.__index = function (t ,key) -- body return 1000 end w = Window.new{x = 10 ,y = 20} print(w.wangbin)打印结果是:1000。这里可以看出,我们在new的时候,w这个表里其实没有wangbin这个元素的,我们重写了元表中的__index,使其返回1000,意思是:如果你要寻找的元素,该表中没有,那么默认返回1000。 二、__newindex的理解
__newindex:当给你的表中不存在的值进行赋值时,lua解释器则会寻找__newindex元方法,发现存在该方法,则执行该方法进行赋值,注意,是使用rawset来进行赋值,至于原因,后面会讲到。
Window.mt = {} function Window.new(o) setmetatable(o ,Window.mt) return o end Window.mt.__index = function (t ,key) return 1000 end Window.mt.__newindex = function (table ,key ,value) if key == "wangbin" then rawset(table ,"wangbin" ,"yes,i am") end end w = Window.new{x = 10 ,y = 20} w.wangbin = "55" print(w.wangbin)ok,这里的打印结果是:yes,i am。w这个表里本来没有wangbin这个元素的,我们重写了元表中__newindex,并在__newindex方法中重新进行赋值操作,然后,我们对这个本不存在的原色w.wangbin进行赋值时,执行__newindex方法的赋值操作,最后,打印结果便是:yes,i am 三、rawget和rawset的理解
rawget是为了绕过__index而出现的,直接点,就是让__index方法的重写无效。(我这里用到"重写"二字,可能不太对,希望能得到纠正)
Window = {} Window.prototype = {x = 0 ,y = 0 ,width = 100 ,height = 100,} Window.mt = {} function Window.new(o) setmetatable(o ,Window.mt) return o end Window.mt.__index = function (t ,key) return 1000 end Window.mt.__newindex = function (table ,key ,value) if key == "wangbin" then rawset(table ,"wangbin" ,"yes,i am") end end w = Window.new{x = 10 ,y = 20} print(rawget(w ,w.wangbin))打印结果是:nil。这里的元表中__index函数就不再起作用了。
但是rawset呢,起什么作用呢?我们再来运行一段代码。
Window = {} Window.prototype = {x = 0 ,y = 0 ,width = 100 ,height = 100,} Window.mt = {} function Window.new(o) setmetatable(o ,Window.mt) return o end Window.mt.__index = function (t ,key) return 1000 end Window.mt.__newindex = function (table ,key ,value) table.key = "yes,i am" end w = Window.new{x = 10 ,y = 20} w.wangbin = "55"然后我们的程序就stack overflow了。可见,程序陷入了死循环。因为w.wangbin这个元素本来就不存在表中,然后这里不断执行进入__newindex,陷入了死循环。
新建一个Movies.plist文件
关键代码:
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UITableViewDataSource,UITabBarDelegate>
@property(nonatomic,retain) NSDictionary *moveTitles;
@property(nonatomic,retain) NSArray *years;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//path to the property list file
NSString *path = [[NSBundle mainBundle] pathForResource:@"Movies" ofType:@"plist"];
//load the list into the dictionary
self.moveTitles = [[NSDictionary alloc] initWithContentsOfFile:path];
//get all the keys in the dictionary object and sort them
self.years = [[self.moveTitles allKeys] sortedArrayUsingSelector:@selector(compare:)];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [self.years count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//check the current year based on the section index
NSString *year = [self.years objectAtIndex:section];
//returns the movies in that year as an array
NSArray *moviesSection = [self.moveTitles objectForKey:year];
return [moviesSection count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] ;
}
//get the year
NSString *year = [self.years objectAtIndex:[indexPath section]];
//get the list of movies for that year
NSArray *moviesSection = [self.moveTitles objectForKey:year];
//get the particular movies based on that row
cell.textLabel.text = [moviesSection objectAtIndex:[indexPath row]];
// Configure the cell...
return cell;
}
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//get the year as the section header
NSString *year = [self.years objectAtIndex:section];
return year;
}
@end
运行结果:
原文:http://chaoyuan.sinaapp.com/?p=451
Style 1: 默认
Toast def = Toast.makeText(this, "default", Toast.LENGTH_SHORT); def.show();
Style 2: 顶部显示
Toast top = Toast.makeText(this, "top", Toast.LENGTH_SHORT); top.setGravity(Gravity.TOP, 0, 0); top.show();
Style 3: 带图片
Toast image = Toast.makeText(this, "with image", Toast.LENGTH_SHORT); LinearLayout toastView = (LinearLayout) image.getView(); toastView.setGravity(Gravity.CENTER_VERTICAL); toastView.setOrientation(LinearLayout.HORIZONTAL); ImageView imageView = new ImageView(this); imageView.setImageResource(R.drawable.ic_launcher); toastView.addView(imageView, 0); image.show();
Style 4: 自定义Layout