ios - 如何在 XCODE 中动态设置带有自定义类的自定义单元格中的 UIVIEW?

标签 ios objective-c uiview

<分区>


想改进这个问题吗? 通过 editing this post 添加细节并澄清问题.

关闭 9 年前

当我在自定义单元格中使用自定义类名创建静态 View 时;它对我来说很好用。我在我的 MainViewController 中使用 CircularProgressView 类作为自定义类。但我想动态创建代表相同的 UIVIEW。如果您有任何想法,请建议我。

谢谢

最佳答案

看看这个.. A Closer Look at Table View Cells

从上面的链接中提取的示例。

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

    static NSString *CellIdentifier = @"ImageOnRightCell";

    UILabel *mainLabel, *secondLabel;
    UIImageView *photo;
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

        mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 220.0, 15.0)];
        mainLabel.tag = MAINLABEL_TAG;
        mainLabel.font = [UIFont systemFontOfSize:14.0];
        mainLabel.textAlignment = UITextAlignmentRight;
        mainLabel.textColor = [UIColor blackColor];
        mainLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
        [cell.contentView addSubview:mainLabel];

        secondLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 20.0, 220.0, 25.0)];
        secondLabel.tag = SECONDLABEL_TAG;
        secondLabel.font = [UIFont systemFontOfSize:12.0];
        secondLabel.textAlignment = UITextAlignmentRight;
        secondLabel.textColor = [UIColor darkGrayColor];
        secondLabel.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
        [cell.contentView addSubview:secondLabel];

        photo = [[UIImageView alloc] initWithFrame:CGRectMake(225.0, 0.0, 80.0, 45.0)];
        photo.tag = PHOTO_TAG;
        photo.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleHeight;
        [cell.contentView addSubview:photo];
    } else {
        mainLabel = (UILabel *)[cell.contentView viewWithTag:MAINLABEL_TAG];
        secondLabel = (UILabel *)[cell.contentView viewWithTag:SECONDLABEL_TAG];
        photo = (UIImageView *)[cell.contentView viewWithTag:PHOTO_TAG];
    }
    NSDictionary *aDict = [self.list objectAtIndex:indexPath.row];
    mainLabel.text = [aDict objectForKey:@"mainTitleKey"];
    secondLabel.text = [aDict objectForKey:@"secondaryTitleKey"];
    NSString *imagePath = [[NSBundle mainBundle] pathForResource:[aDict objectForKey:@"imageKey"] ofType:@"png"];
    UIImage *theImage = [UIImage imageWithContentsOfFile:imagePath];
    photo.image = theImage;

    return cell;
}

关于ios - 如何在 XCODE 中动态设置带有自定义类的自定义单元格中的 UIVIEW?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20919837/

上一篇:ios - 在自定义单元格中的按钮中设置标签时出现异常

下一篇:iOS - segue 和 tableView

相关文章:

objective-c - 将触摸区域添加到 UIView (iOS5)

ios - 构造具有唯一中心的 UIView 对象的 NSArray

ios - 自动布局不适用于 iOS7 和 Xcode 6

ios - Objective-C : Print/Log an array as usable code?

objective-c - 使用 C++ 方法作为 ObjC 选择器?

ios - callkit来电语音问题

ios - 如何将类型 'UIImage?' 的值分配给类型 'UIView?'

ios - 如何在 ios 中执行连续方向检测?

ios - 如何使用 iOS swift hero 框架在两个 View Controller 之间进行转换?

ios - 执行drawAtPoint时如何指定图像尺寸/大小?