我已经设置了一个UITableView,它有3个部分,每个部分有3行。我使用以下代码来指定我希望某些数组中的对象显示为行内容;
recipeData = [[NSArray alloc] initWithObjects:@"Chicken & Veg Rissoto",@"Super Healthy Pizza",@"Salmon Burgers",nil];
dessertsData = [[NSArray alloc] initWithObjects:@"Raspberry Parfaits",@"ChocNana YumYum",@"Grilled Choc Sandwich",nil];
beveragesData = [[NSArray alloc] initWithObjects:@"Berry Smoothie",@"Banana Berry Smoothie",@"Cocoa Soy Smoothie",nil];
[self setTitle:@"Recipe Table"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.recipeData count];
return [self.dessertsData count];
return [self.beveragesData count];
这一切都运转正常,直到今晚我在行中文字左侧添加了图片。图像显示完美,但由于某种原因,行全部填充了来自beveragesData的对象,它完全忽略了其他两组对象。代码低于我使用过的代码。我希望它只是简单地编写if语句就可以了。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) { cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:SimpleTableIdentifier] autorelease];
}
if(indexPath.section == 0)
if(indexPath.row == 0)
[cell.imageView setImage:[UIImage imageNamed:@"Main0.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
if(indexPath.section == 0)
if(indexPath.row == 1)
[cell.imageView setImage:[UIImage imageNamed:@"Main1.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
if(indexPath.section == 0)
if(indexPath.row == 2)
[cell.imageView setImage:[UIImage imageNamed:@"Main2.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
if(indexPath.section == 1)
if(indexPath.row == 0)
[cell.imageView setImage:[UIImage imageNamed:@"Dessert0.png"]];
[cell.textLabel setText:[dessertsData objectAtIndex:indexPath.row]];
if(indexPath.section == 1)
if(indexPath.row == 1)
[cell.imageView setImage:[UIImage imageNamed:@"Dessert1.png"]];
[cell.textLabel setText:[dessertsData objectAtIndex:indexPath.row]];
if(indexPath.section == 1)
if(indexPath.row == 2)
[cell.imageView setImage:[UIImage imageNamed:@"Dessert2.png"]];
[cell.textLabel setText:[dessertsData objectAtIndex:indexPath.row]];
if (indexPath.section == 2)
if(indexPath.row == 0)
[cell.imageView setImage:[UIImage imageNamed:@"Drink0.png"]];
[cell.textLabel setText:[beveragesData objectAtIndex:indexPath.row]];
if(indexPath.section == 2)
if(indexPath.row == 1)
[cell.imageView setImage:[UIImage imageNamed:@"Drink1.png"]];
[cell.textLabel setText:[beveragesData objectAtIndex:indexPath.row]];
if(indexPath.section == 2)
if(indexPath.row == 2)
[cell.imageView setImage:[UIImage imageNamed:@"Drink2.png"]];
[cell.textLabel setText:[beveragesData objectAtIndex:indexPath.row]];
return cell;
所以目前,当运行时,UITableView的所有9行都填充了beveragesData中的对象。任何帮助将不胜感激。
由于
答案 0 :(得分:1)
Objective-c结构不是基于缩进
此:
if(indexPath.section == 0)
if(indexPath.row == 0)
[cell.imageView setImage:[UIImage imageNamed:@"Main0.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
应该是:
if(indexPath.section == 0)
if(indexPath.row == 0) {
[cell.imageView setImage:[UIImage imageNamed:@"Main0.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
}
就像C / C ++ / Java - if适用于下一个语句。如果需要多个,则需要将语句括在花括号中。你也可以把所有的时间放在一起,不用担心。
此外,此代码
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.recipeData count];
return [self.dessertsData count];
return [self.beveragesData count];
}
不按照您的想法行事。第一次返回总是运行,后两行被忽略(应该已经发出警告)。
答案 1 :(得分:1)
您的numberOfRowsInSection:
部分也不正确;应该是:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if ( section == 0 )
return [self.recipeData count];
if ( section == 1 )
return [self.dessertsData count];
if ( section == 3 )
return [self.beveragesData count];
}
答案 2 :(得分:0)
您还应该使用switch
声明:
switch ( indexPath.section )
{
case 0:
{
if(indexPath.row == 0) {
[cell.imageView setImage:[UIImage imageNamed:@"Main0.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
}
if(indexPath.row == 1) {
[cell.imageView setImage:[UIImage imageNamed:@"Main1.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
}
if(indexPath.row == 2) {
[cell.imageView setImage:[UIImage imageNamed:@"Main2.png"]];
[cell.textLabel setText:[recipeData objectAtIndex:indexPath.row]];
}
}
}
/* etc. */