实现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