【问题标题】:sectioned UITableView分段的 UITableView
【发布时间】:2009-08-19 10:13:12
【问题描述】:

我正在为 iPhone 制作一个应用程序,其中我正在创建包含 2 个部分的 UITableView。我已经创建了 UITableView 的部分,但问题是我需要在每个部分的每个单元格中使用不同的图像。有没有人对此有任何解决方案

【问题讨论】:

    标签: objective-c iphone xcode uitableview


    【解决方案1】:

    是的,这很容易。你的数据源中有这个吗?

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

    添加以下代码:

    NSString *CellIdentifier = [NSString stringWithFormat:@"cell_%i",indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        if (indexPath.section == 0 && indexPath.row == 0){
            [cell.imageView setImage:[UIImage imageNamed:@"justanimage.png"]];
        }else if (indexPath.section == 0 && indexPath.row == 1){
            [cell.imageView setImage:[UIImage imageNamed:@"justanimage1.png"]];
        }else if (indexPath.section == 1 && indexPath.row == 0){
            [cell.imageView setImage:[UIImage imageNamed:@"justanimage2.png"]];
        }else if (indexPath.section == 1 && indexPath.row == 1){
            [cell.imageView setImage:[UIImage imageNamed:@"justanimage3.png"]];
        }
    }
    

    您也可以将数据保存在 NSMutableArray 中,但这有点复杂。

    【讨论】:

    • 嗨,蒂姆,我已经尝试过你的代码,但它在 [cell.imageView setimage:.....] 的每一行中都给了我 4 个错误“imageView 不是结构或联合”
    • 好吧,在这种情况下,我猜你有一个旧版本的 iPhone SDK。你有3.0或更高版本吗?否则,您必须执行以下操作: [cell setImage:... 并更改 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];在单元格 = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    • 感谢我早先使用 2.2.1。 dts 为什么它给我错误。它在 3.0 中完美运行
    • nw,我想将选定的图像显示到下一个视图。我该怎么做
    • - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { UIViewController *vc = [[UIViewController alloc] init]; UIImageView *iv = [[UIImageView alloc] initWithImage:[[tableView cellForRowAtIndexPath:indexPath].imageView image]]; [vc setView:iv]; [yourNavController pushViewController:vc 动画:TRUE]; }
    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多