【问题标题】:tableview image not appearing properly - iOS 8 iPhone App Objective Ctableview 图像未正确显示 - iOS 8 iPhone App Objective C
【发布时间】:2015-11-20 01:58:53
【问题描述】:

我正在创建一个带有两个 NSArray 的简单 tableView。第一个数组包含标题,第二个数组包含图像名称。一切正常。但是行分隔符在表格视图中的图像之后出现一些空格(参见第一个屏幕截图),而它应该没有空格出现(如第二个屏幕截图)。

First Screenshot

Second screenshot

我尝试了不同的方法,但遇到了同样的问题(在 iOS 8 设备上测试)。

我做错了什么?

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

// Return the number of sections.
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

// Return the number of rows in the section.
return [dataSourceArray count];
}


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

// "Cell" is the default cell identifier in a new Master-Detail Project
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}


  cell.textLabel.text = [dataSourceArray objectAtIndex:indexPath.row];

//get UIImage image Names from imageArray
cell.imageView.image=[UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];



return cell;
}

【问题讨论】:

标签: objective-c iphone uitableview ios8 tableviewcell


【解决方案1】:

我看不到你的图片.. 也许您没有使用约束或者您的单元格很小,您需要设置单元格的高度才能查看图像。

【讨论】:

  • 在第一个屏幕截图中,绿色方块是我的图像(我正在使用的示例图像)。大小为 44x44,tableview 单元格的高度也是 44。
  • 对不起,我看不到图像.. 你用过 cell.imageView.image = [UIImage imageNamed:@""];
  • 是的,cell.imageView.image=[UIImage imageNamed:[imageArray objectAtIndex:indexPath.row]];其中 imageArray 包含 imageNames
  • 您可以使用它。 NSDictionary NSDictionary *dicName = [self.arrayScreen objectAtIndex:indexPath.row]; cell.textLabel.text = [dicName objectForKey:@"name"]; cell.imageView.image = [UIImage imageNamed:[dicName objectForKey:@"nameimage"]];
  • 感谢 Jeff 的时间和帮助。我真的很感谢你的努力。
【解决方案2】:

尝试使用 NSDictionary

NSMutableArray *item;

- (void)viewDidLoad {
    [super viewDidLoad];

 self.item = [NSMutableArray array];

NSDictionary *item1 =  @{@"dicNameItem":@"NameItem",@"dicImageItem":@"ImageItem"};
[self.arrayScreen addObject:item1];

}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.arrayScreen count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellScreen" forIndexPath:indexPath];

    NSDictionary *dicName = [self.arrayScreen objectAtIndex:indexPath.row];
    cell.textLabel.text = [dicName objectForKey:@"dicNameItem"];
    cell.imageView.image  = [UIImage imageNamed:[dicName objectForKey:@"dicImageItem"]];

    return cell;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多