cell

首先坑雅,講解一下本次為大家分享的主要內(nèi)容 在tablecell中實現(xiàn)點菜功能,具體功能如下


viewcontroll

.h

@property(nonatomic,strong)UILabel *theNameLab;

@property(nonatomic,strong)UILabel *theNumLab;

.m

{

NSMutableArray *data;

int num;//總價格

UITableView *NameTable;

}

- (void)viewDidLoad {

[super viewDidLoad];

self.navigationItem.title = @"菜譜";

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Therecipe" ofType:@"plist"];

data = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];

NameTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)style:UITableViewStyleGrouped];

NameTable.delegate? = self;

NameTable.dataSource = self;

NameTable.rowHeight = 120;

NameTable.backgroundColor = [UIColor whiteColor];

[self.view addSubview:NameTable];

_theNameLab = [[UILabel alloc]initWithFrame:CGRectMake(10, self.view.frame.size.height-50, 100, 30)];

_theNameLab.text = @"總價格為:";

[self.view addSubview:self.theNameLab];

_theNumLab = [[UILabel alloc]initWithFrame:CGRectMake(110, self.view.frame.size.height-50, 100, 30)];

num = 0;

NSString *stq = [[NSString alloc]initWithFormat:@"%d",num];

_theNumLab.text = stq;

[self.view addSubview:self.theNumLab];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return data.count;

}

//表格cell

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

num = 0;

static NSString *Str = @"Cell";

NameTableViewCell *theTVC = [tableView dequeueReusableCellWithIdentifier:Str];

if (!theTVC) {

theTVC = [[NameTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

}

theTVC.ImageView.image = [UIImage imageNamed:[data[indexPath.row]objectForKey:@"ImageView"]];

theTVC.NameLab.text =[data[indexPath.row]objectForKey:@"Name"];

theTVC.PriceLab.text = [data[indexPath.row]objectForKey:@"price"];

theTVC.NumLab.text =[data[indexPath.row]objectForKey:@"Num"];

theTVC.ReductionBut.tag = indexPath.row;

[theTVC.ReductionBut addTarget:self action:@selector(RedyctClick:) forControlEvents:UIControlEventTouchUpInside];

theTVC.addBut.tag = indexPath.row;

[theTVC.addBut addTarget:self action:@selector(AddClick:) forControlEvents:UIControlEventTouchUpInside];

NSString *theS = [[data objectAtIndex:indexPath.row]objectForKey:@"price"];

int jiage = [theS intValue];

NSString *theSQ =[[data objectAtIndex:indexPath.row]objectForKey:@"Num"];

int Geshu = [theSQ intValue];

num = num +jiage*Geshu;

NSLog(@"%d",num);

_theNumLab.text = [NSString stringWithFormat:@"%d",num];

return theTVC;

}

//減號

-(void)RedyctClick:(UIButton *)sender

{

//修改字典里面的內(nèi)容,先按照結構取到你想修改內(nèi)容的小字典

NSMutableDictionary *dd = [data objectAtIndex:sender.tag];

NSString *dataStr = [[data objectAtIndex:sender.tag]objectForKey:@"Num"];

int numo = [dataStr intValue];

if (numo>0) {

numo = numo-1;

NSString *numStr = [NSString stringWithFormat:@"%d",numo];

[dd setObject:numStr forKey:@"Num"];

[data removeObjectAtIndex:sender.tag];

[data addObject:dd];

dispatch_async(dispatch_get_main_queue(), ^{

[NameTable reloadData];

});

}

}

//加號

-(void)AddClick:(UIButton *)sender

{

//修改字典里面的內(nèi)容,先按照結構取到你想修改內(nèi)容的小字典

NSMutableDictionary *dd = [data objectAtIndex:sender.tag];

NSString *dataStr = [[data objectAtIndex:sender.tag]objectForKey:@"Num"];

int numo = [dataStr intValue];

numo = [dataStr intValue];

numo = numo+1;

NSString *numStr = [NSString stringWithFormat:@"%d",numo];

[dd setObject:numStr forKey:@"Num"];

[data removeObjectAtIndex:sender.tag];

[data addObject:dd];

dispatch_async(dispatch_get_main_queue(), ^{

[NameTable reloadData];

});

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

TwoViewController *theTwo = [[TwoViewController alloc]init];

theTwo.theImageStr = [NSString stringWithFormat:@"%@",[[data objectAtIndex:indexPath.row]objectForKey:@"ImageView"]];

theTwo.theLabStr =[NSString stringWithFormat:@"%@",[[data objectAtIndex:indexPath.row]objectForKey:@"Name"]];

[self.navigationController pushViewController:theTwo animated:YES];

}

TwoViewControll

.h定義屬性

@property(nonatomic,strong)UIImageView *theImage;

@property(nonatomic,strong)UILabel *theLab;

@property(nonatomic,strong)NSString *theImageStr;

@property(nonatomic,strong)NSString *theLabStr;

.m實現(xiàn)

self.view.backgroundColor = [UIColor whiteColor];

_theImage = [[UIImageView alloc]initWithFrame:CGRectMake(30, 100, self.view.frame.size.width-60,self.view.frame.size.width-60 )];

[self.view addSubview:self.theImage];

_theImage.image = [UIImage imageNamed:_theImageStr];

_theLab = [[UILabel alloc]initWithFrame:CGRectMake(30, self.view.frame.size.width+60, self.view.frame.size.width-60,30 )];

_theLab.backgroundColor = [UIColor yellowColor];

[self.view addSubview:self.theLab];

_theLab.text = _theLabStr;

_theLab.textAlignment = NSTextAlignmentCenter;

NameTableView

cell.h

@property(nonatomic,strong)UIImageView *ImageView;

@property(nonatomic,strong)UILabel *NameLab;

@property(nonatomic,strong)UILabel *PriceLab;

@property(nonatomic,strong)UIButton *ReductionBut;

@property(nonatomic,strong)UIButton *addBut;

@property(nonatomic,strong)UILabel *NumLab;

cell.m

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

self = [super initWithStyle: style reuseIdentifier:reuseIdentifier];

if (self) {

_ImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 80, 80)];

_NameLab? = [[UILabel alloc]initWithFrame:CGRectMake(10, 95, 80, 20)];

_PriceLab = [[UILabel alloc]initWithFrame:CGRectMake(110, 40, 60, 40)];

[self addSubview:self.ImageView];

[self addSubview:self.NameLab];

[self addSubview:self.PriceLab];

_ReductionBut = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width-100, 45, 30, 30)];

[_ReductionBut setTitle:@"-" forState:UIControlStateNormal];

[_ReductionBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[self addSubview:self.ReductionBut];

_NumLab = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width-70, 45, 40, 30)];

_NumLab.textAlignment = NSTextAlignmentCenter;

_NumLab.backgroundColor = [UIColor yellowColor];

[self addSubview:self.NumLab];

_addBut = [[UIButton alloc]initWithFrame:CGRectMake(self.frame.size.width-30, 45, 30, 30)];

[_addBut setTitle:@"+" forState:UIControlStateNormal];

[_addBut setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[self addSubview:self.addBut];

}

return self;

}

其中的plist文件:


以上就是本cell其中的內(nèi)容,希望對大家有所幫助遥诉,感謝朋友們的支持?

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市矮锈,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌苞笨,老刑警劉巖债朵,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件瀑凝,死亡現(xiàn)場離奇詭異序芦,居然都是意外死亡猜丹,警方通過查閱死者的電腦和手機芝加,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進店門射窒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來将塑,“玉大人脉顿,你說我怎么就攤上這事“保” “怎么了?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵蔽莱,是天一觀的道長弟疆。 經(jīng)常有香客問我盗冷,道長怠苔,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任柑司,我火速辦了婚禮,結果婚禮上锅劝,老公的妹妹穿的比我還像新娘攒驰。我一直安慰自己,他們只是感情好故爵,可當我...
    茶點故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布玻粪。 她就那樣靜靜地躺著,像睡著了一般诬垂。 火紅的嫁衣襯著肌膚如雪奶段。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天剥纷,我揣著相機與錄音痹籍,去河邊找鬼。 笑死晦鞋,一個胖子當著我的面吹牛蹲缠,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播悠垛,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼线定,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了确买?” 一聲冷哼從身側(cè)響起斤讥,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎湾趾,沒想到半個月后芭商,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡搀缠,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了簸州。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鉴竭。...
    茶點故事閱讀 40,090評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡岸浑,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出矢洲,到底是詐尸還是另有隱情璧眠,我是刑警寧澤兵钮,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站掘譬,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏葱轩。R本人自食惡果不足惜睦焕,卻給世界環(huán)境...
    茶點故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一靴拱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧袜炕,春花似錦、人聲如沸偎窘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至仆葡,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間沿盅,已是汗流浹背把篓。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工嗡呼, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人南窗。 一個月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓,卻偏偏與公主長得像万伤,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子敌买,可洞房花燭夜當晚...
    茶點故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容