TableView实现2列

实现2列。每一个cell里放入2个model


点击每个model的button跳入对应的detail页面

写的比较麻烦。。。。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
	int tCount = [arrFood_ count];
	if (tCount%2 == 0 ) {
		return [arrFood_ count]/2;
	}else {
		return [arrFood_ count]/2 +1;
	}

    
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	
    if ([arrFood_ count]%2 ==0) {
		static NSString *CellIdentifier = @"Cell1";
		DoubleFoodCell *cell = (DoubleFoodCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
		
		if (cell == nil) {
			
			NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DoubleFood" owner:self options:nil];									  
			cell =  [nib objectAtIndex:0];
			NSInteger row = [indexPath row];
			Food *food0 = [arrFood_ objectAtIndex:row*2];
			cell.lblName1.text = [food0 name];
			cell.lblCount1.text = [food0 count];
			[cell.btn1 setTag:[[food0 ID] intValue]];
			[cell.btn1 addTarget:self action:@selector(showFood:) forControlEvents:UIControlEventTouchUpInside];
			
			Food *food1 = [arrFood_ objectAtIndex:row*2+1];
			cell.lblName2.text = [food1 name];
			cell.lblCount2.text = [food1 count];
			[cell.btn2 setTag:[[food1 ID] intValue]];
			[cell.btn2 addTarget:self action:@selector(showFood:) forControlEvents:UIControlEventTouchUpInside];

		}
		return cell;
	}
	if ([indexPath row]*2+1 == [arrFood_ count]) {
		static NSString *CellIdentifier = @"Cell2";
		SingleFoodCell *cell = (SingleFoodCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
		
		if (cell == nil) {
			
			NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SingleFood" owner:self options:nil];									  
			cell =  [nib objectAtIndex:0];
			NSInteger row = [indexPath row];
			Food *food0 = [arrFood_ objectAtIndex:row*2];
			cell.lblName1.text = [food0 name];
			cell.lblCount1.text = [food0 count];
			[cell.btn1 setTag:[[food0 ID] intValue]];
			[cell.btn1 addTarget:self action:@selector(showFood:) forControlEvents:UIControlEventTouchUpInside];
			
			
			
		}
		return cell;
	}else {
		static NSString *CellIdentifier = @"Cell1";
		DoubleFoodCell *cell = (DoubleFoodCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
		
		if (cell == nil) {
			
			NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"DoubleFood" owner:self options:nil];									  
			cell =  [nib objectAtIndex:0];
			NSInteger row = [indexPath row];
			Food *food0 = [arrFood_ objectAtIndex:row*2];
			cell.lblName1.text = [food0 name];
			cell.lblCount1.text = [food0 count];
			[cell.btn1 setTag:[[food0 ID] intValue]];
			[cell.btn1 addTarget:self action:@selector(showFood:) forControlEvents:UIControlEventTouchUpInside];
			
			Food *food1 = [arrFood_ objectAtIndex:row*2+1];
			cell.lblName2.text = [food1 name];
			cell.lblCount2.text = [food1 count];
			[cell.btn2 setTag:[[food1 ID] intValue]];
			[cell.btn2 addTarget:self action:@selector(showFood:) forControlEvents:UIControlEventTouchUpInside];
			
		}
		return cell;
	}

}


源码下载地址:http://download.csdn.net/source/3540787





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值