2014-04-21 78 views
0

我想为UITableView使用自定义accessoryView。至于accessorView我正在使用UIImageView。我面临的问题是accessoryView只出现在最后一行UITableViewUITableView自定义accessoryView只出现在最后一个单元格

In viewDidLoad我初始化了UIImageView。

customAccessory = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 15)]; 
customAccessory.image = [UIImage imageNamed:@"icon_right"]; 

cellForRowAtIndexPath我这样做:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    MyDayCell *cell =(MyDayCell *)[self.tableView dequeueReusableCellWithIdentifier:@"MyDayCell"]; 
    if (cell == nil) { 
     NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed:@"MyDayCell" owner:self options:nil]; 
     cell = [cellArray objectAtIndex:0]; 
    } 
    switch (indexPath.row) { 
     case 0: 
      cell.image.image = [UIImage imageNamed:@"icon_guide"]; 
      break; 
     case 1: 
      cell.image.image = [UIImage imageNamed:@"icon_media"]; 
      break; 
     case 2: 
      cell.image.image = [UIImage imageNamed:@"icon_lives"]; 
      break; 
     case 3: 
      cell.image.image = [UIImage imageNamed:@"icon_ask"]; 
      break; 
     case 4: 
      cell.image.image = [UIImage imageNamed:@"icon_k"]; 
      break; 
     default: 
      break; 
    } 
    cell.name.text = [dataSource objectAtIndex:indexPath.row]; 
    cell.accessoryView = customAccessory; 
    return cell; 
} 

这是截图:

enter image description here

+0

您在所有单元格中使用了相同的对象'customAccessory',因此它将显示在最后一个单元格中。为每个单元创建附件视图将解决该问题。 – Akhilrajtr

+0

是的。但为什么会发生?为什么我不能为所有单元使用相同的accessoryView? – Rashad

+5

视图只能有一个父级。无法同时多次显示单个视图实例。 – rmaddy

回答

0

还有就是customAccessory只有一个实例。你应该为每个单元格创建UIImageView。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    MyDayCell *cell =(MyDayCell *)[self.tableView dequeueReusableCellWithIdentifier:@"MyDayCell"]; 
    if (cell == nil) { 
     NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed:@"MyDayCell" owner:self options:nil]; 
     cell = [cellArray objectAtIndex:0]; 

     customAccessory = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 15)]; 
     customAccessory.image = [UIImage imageNamed:@"icon_right"]; 
     cell.accessoryView = customAccessory; 

    } 

    // Here your code... 
    // An return cell 
     return cell; 
} 
1

试试这个,

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

    .... 
    if (cell == nil) { 
     NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed:@"MyDayCell" owner:self options:nil]; 
     cell = [cellArray objectAtIndex:0]; 
     UIImageView *customAccessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 15)]; 
     customAccessoryView.image = [UIImage imageNamed:@"icon_right"]; 
     cell.accessoryView = customAccessoryView; 
    } 

    .... 
} 
+0

它的工作原理,但你能解释为什么会发生这种情况? – Rashad

+0

视图只能包含在单个父视图的层次结构中。只要将它添加到新的,它就会从前一个中删除。在这里,当你将视图添加到单元格时,它将从前一个单元格中移除。 – Akhilrajtr

2

视图只能有一个上海华。当您将自定义视图添加到第二个单元格时,它将从第一个单元格中移除。因此,为每个表视图单元格创建自定义附件视图。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    MyDayCell *cell =(MyDayCell *)[self.tableView dequeueReusableCellWithIdentifier:@"MyDayCell"]; 
    if (cell == nil) { 
     NSArray *cellArray = [[NSBundle mainBundle] loadNibNamed:@"MyDayCell" owner:self options:nil]; 
     cell = [cellArray objectAtIndex:0]; 
    } 
    switch (indexPath.row) { 
     case 0: 
      cell.image.image = [UIImage imageNamed:@"icon_guide"]; 
      break; 
     case 1: 
      cell.image.image = [UIImage imageNamed:@"icon_media"]; 
      break; 
     case 2: 
      cell.image.image = [UIImage imageNamed:@"icon_lives"]; 
      break; 
     case 3: 
      cell.image.image = [UIImage imageNamed:@"icon_ask"]; 
      break; 
     case 4: 
      cell.image.image = [UIImage imageNamed:@"icon_k"]; 
      break; 
     default: 
      break; 
    } 
    cell.name.text = [dataSource objectAtIndex:indexPath.row]; 
    UIImageView *customAccessory = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 10, 15)]; 
    customAccessory.image = [UIImage imageNamed:@"icon_right"]; 
    cell.accessoryView = customAccessory; 
    return cell; 
} 
相关问题