[英]Adding images to a table view on left side
我创建了一个表格单元格,我希望在左侧添加图像。要为我创建的所有单元格提供图像。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell==nil) {
cell=[[UITableViewCell alloc]init];
}
if([indexPath row] == 0){
cell.textLabel.text=@"mano";
cell.backgroundColor = [UIColor redColor];
}
if([indexPath row] == 1){
cell.textLabel.text=@"happy";
cell.backgroundColor=[UIColor whiteColor];
}
if([indexPath row] == 2){
cell.textLabel.text=@"welcome";
cell.backgroundColor=[UIColor grayColor];
}
if([indexPath row] == 3)
{
cell.textLabel.text=@"enjoy";
cell.backgroundColor=[UIColor magentaColor];
}
return cell;
}
放入cellforrowatindexpath。
cell.imageView.image = [UIImage imageNamed:@"imageName.png"];
在cellForRowAtIndexPath方法中
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
接着
NSString *file = [[NSBundle mainBundle] pathForResource:[self.photos objectAtIndex:self.currentIndex] ofType:nil];
cell.imageView.image = [UIImage imageWithContentsOfFile:file]
代替[UiImage imageNamed:@“”]使用[UIImage imageWithContentsOfFile:] 。
由于ImageNamed占用过多内存(请检查此链接:-http: //www.jorambarrez.be/blog/2012/04/19/ios-imagenamed-vs-imagewithcontentsoffile/ )。
希望这对你有帮助。
使用此代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell==nil) {
cell=[[UITableViewCell alloc]init];
}
if([indexPath row] == 0){
cell.textLabel.text=@"mano";
cell.backgroundColor = [UIColor redColor];
cell.imageView.image = [UIImage imageNamed:@"arrow0.png"];
}
if([indexPath row] == 1){
cell.textLabel.text=@"happy";
cell.backgroundColor=[UIColor whiteColor];
cell.imageView.image = [UIImage imageNamed:@"arrow1.png"];
}
if([indexPath row] == 2){
cell.textLabel.text=@"welcome";
cell.backgroundColor=[UIColor grayColor];
cell.imageView.image = [UIImage imageNamed:@"arrow2.png"];
}
if([indexPath row] == 3)
{
cell.textLabel.text=@"enjoy";
cell.backgroundColor=[UIColor magentaColor];
cell.imageView.image = [UIImage imageNamed:@"arrow3.png"];
}
return cell;
}
imageView是UIImageView的出口
首先在tableview中显示图像,无需使用自定义单元格。 首先,使您的tableviewcell基本。 分配tableviewcell重用标识符。 为tableviewcell编写此代码
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyIdentifier"];
}
cell.imageView.image = [UIImage imageNamed:@"image.png"];
cell.backgroundColor=[UIColor magentaColor];
return cell;
}
If you have used storyboard then no need to check if cell is nil. It intializes cell automatically when dequeing is not possible. Please check this tutorial.
http://www.raywenderlich.com/50308/storyboards-tutorial-in-ios-7-part-1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.