UITableView的第一行不同

时间:2012-02-03 10:09:25

标签: iphone ios uitableview uiimage

我需要一个UITableView,其第一行应显示徽标,而其他行应显示其他表数据。 我尝试了以下代码:

if(indexPath.row == 0) {
    //Code for imageview
} else {
    //Code to display array content
}

我的问题是我没有在表视图中获得第一个数组项。

编辑::

if (indexPath.row == 0) {

        CGRect myImageRect = CGRectMake(120.0f, 5.0f, 70.0f, 55.0f);
        UIImageView *myImage = [[UIImageView alloc] initWithFrame:myImageRect];
        [myImage setImage:[UIImage imageNamed:@"logo.png"]];
        [cell.contentView addSubview:myImage];
    }
    else  {

       NSDictionary *dict = [menuitems objectAtIndex:indexPath.row];

        cell.textLabel.text =[dict objectForKey:@"category"]; 
       }

3 个答案:

答案 0 :(得分:8)

尝试使用标题视图。使用以下代码将徽标放在行的顶部。

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    UIImageView *myimgvw = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
    [myimgvw setImage:[UIImage imageNamed:@"logo.png"]];
    [myView addSubview:myimgvw];

    return myView; 
}

答案 1 :(得分:1)

您尚未显示显示数组内容的代码,但我猜您使用的是

[dataArray objectAtIndex:indexPath.row];

但是因为你在第0行显示图像,你需要将它偏移-1。换句话说,表中的第二行将具有indexPath.row = 1,但您需要将其作为objectAtIndex:0。

答案 2 :(得分:0)

然后你需要使用indexPath-1在上面代码片段的else部分中显示文本而不是indexPath。