Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 自定义UITableViewCell_Iphone_Uitableview - Fatal编程技术网

Iphone 自定义UITableViewCell

Iphone 自定义UITableViewCell,iphone,uitableview,Iphone,Uitableview,如何自定义UITableviewCell,因为我希望在单个单元格中看到标签、日期和图片。有两个选项(可能更多)。您可以使用本机UITableViewCell属性向单元格添加内容,或创建自定义单元格(我的意思是,向单元格添加您自己的子视图)。开始尝试第一个,它简单优雅,效果会很好。例如,请尝试以下单元格创建方法: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)

如何自定义UITableviewCell,因为我希望在单个单元格中看到标签、日期和图片。

有两个选项(可能更多)。您可以使用本机UITableViewCell属性向单元格添加内容,或创建自定义单元格(我的意思是,向单元格添加您自己的子视图)。开始尝试第一个,它简单优雅,效果会很好。例如,请尝试以下单元格创建方法:

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

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        // notice the Style. The UITableViewCell has a few very good styles that make your cells look very good with little effort
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
    }

    // Configure the cell...
    // In my case I get the data from the elements array that has a bunch on dictionaries
    NSDictionary *d = [elements objectAtIndex:indexPath.row];

    // the textLabel is the main label
    cell.textLabel.text = [d objectForKey:@"title"];

    // the detailTextLabel is the subtitle
    cell.detailTextLabel.text = [d objectForKey:@"date"];

    // Set the image on the cell. In this case I load an image from the bundle
    cell.imageView.image = [UIImage imageNamed:@"fsaint.png"];

    return cell;
}

您可以使用或通过自定义UITableViewCell查看此链接

在这里,您将了解如何使用Interface Builder创建自定义单元格,并在应用程序的XCode中使用它


我非常喜欢重写UITableViewCell类,并在self.contentView中进行自定义绘图。这项技术稍微复杂一点,但它会带来更好的滚动性能

例如,假设您覆盖单元格,并在其上具有3个属性,如下所示:

@property(nonatomic, retain) UIImage *userPic;
@property(nonatomic, retain) NSString *label;
@property(nonatomic, retain) NSString *date;
然后,可以使用(drawRect:)函数在单元格中绘制它们:

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];
    [userPic drawInRect: CGRectMake(10, 5, 50, 50)];
    [label drawAtPoint:CGPointMake(70, 5) withFont:[UIFont boldSystemFontOfSize:17]];
    [date drawAtPoint:CGPointMake(70, 30) withFont:[UIFont systemFontOfSize:14]];
  }
有关更多示例,请尝试查看此使用此样式的框架: