簡體   English   中英

在UITableView中突出顯示所選單元格的邊框

[英]Highlight the borders of the selected cell in UITableView

是否有可能在Objective-c中的UITableViewController突出顯示 所選單元格邊框

您可以嘗試使用setSelectedBackgroundView:為選擇創建自定義UIView / UIImageView setSelectedBackgroundView:

這是我在自定義tableviewcell中用於自定義漸變的示例代碼:

UIView *selctionView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height)] autorelease];

CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = selctionView.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor blueColor] CGColor], (id)[[UIColor redColor] CGColor], nil];

[selctionView.layer insertSublayer:gradient atIndex:0];

[self setSelectedBackgroundView:selctionView];

編輯:

我發現你也可以使用這些方法:

[test.layer setBorderColor: [[UIColor redColor] CGColor]];
[test.layer setBorderWidth: 1.0]; 

對於圖層。

一定要導入QuartzCore.h

整個桌面視圖的其他內容:

[tableViewController.tableView setSeparatorColor:[UIColor redColor]];

這非常簡單,因為OS 3.0只是在willDisplayCell方法中設置單元格的背景顏色。 您不能在cellForRowAtIndexPath中設置顏色。

這適用於普通和分組樣式:

碼:

  • (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {cell.backgroundColor = [UIColor redColor]; }

PS:這里是willDisplayCell的文檔摘錄:

"A table view sends this message to its delegate just before it uses cell to draw a row, thereby permitting the delegate to customize the cell object before it is displayed. This method gives the delegate a chance to override state-based properties set earlier by the table view, such as selection and background color. After the delegate returns, the table view sets only the alpha and frame properties, and then only when animating rows as they slide in or out."

我在colionel的這篇文章中找到了這些信息。 謝謝他!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM