Cell选中问题 以及 URL中文编码

本文解决两个常见iOS开发问题:如何自定义UITableViewCell选中样式而不改变背景颜色,以及如何正确处理URL中的中文编码问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记录开发遇到的两个问题,以及解决方案


1、 UITableViewCell的选中问题

总所周知,cell的选中样式有三种,UITableViewCellSelectionStyleNone、UITableViewCellSelectionStyleGray、
UITableViewCellSelectionStyleBlue。
另外,UITableViewCellSelectionStyleBlue这也属性在iOS7以后已经不起作用了。

下面这种情况下,cell在选中的时候,会把子视图的背景颜色换成默认的淡蓝色。

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewCellEditingStyleDelete | UITableViewCellEditingStyleInsert;
}

这种cell多选模式下,如果设置了cell的选中样式为None,那我们就无法对cell进行选择了。

这个时候,想即设置了cell的选中样式,又不会被选中后改变背景颜色,就可以重写下面的两个方法。把被改变颜色的子视图设置为你想要的效果。

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    [super setHighlighted:highlighted animated:animated];
    self.progressView.centerLabel.backgroundColor = [UIColor clearColor];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    self.progressView.centerLabel.backgroundColor = [UIColor clearColor];
}

2、URL的编码问题

对于URL里包含中文的,一般我们会采取下面这种方式进行转码。

NSString *routing = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

但是,对于中文已经被转码过的,里面包含了转义字符。如果我们再次用这种方式转码,就会出问题。

所以我们应该对包含中文字符和不包含中文字符的URL进行区分,下面的一个方法很好的解决了这个问题。

NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                                                        (CFStringRef)self.videoUrl,
                                                                                                        (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]",
                                                                                                        NULL,
                                                                                                        kCFStringEncodingUTF8));
def parsePage(html_content, authorName_list, likeNr_list, URL_list, userURL_list, num): """ 解析网页内容并更新数据列表。 Args: html_content (str): 当前页面的HTML内容 authorName_list (list): 存储作者名字的列表 likeNr_list (list): 存储获赞数量的列表 URL_list (list): 存储笔记URL的列表 userURL_list (list): 存储用户URL的列表 qbar (tqdm): 进度条对象 num (int): 需要爬取的笔记数量 Returns: None: 数据存储在传入的列表中 """ response = Selector(text=html_content) divs = response.xpath('//div[contains(@class, "feeds-container")]/section/div')# 选中网页中包含笔记信息的部分 # 遍历divs获取每一篇笔记的信息 for div in divs: if len(URL_list) >= num: break if div.xpath('.//span[contains(text(), "大家都在搜")]'): continue # 择并提取网页数据 try: author_name = div.xpath('.//a[contains(@class, "author")]/span[contains(@class, "name")]/text()').get()# 作者名字 like_nr = div.xpath('.//span[contains(@class, "count")]/text()').get()# 获赞数量 url = div.xpath('.//a[contains(@class, "cover")]/@href').get()# 笔记URL user_url = div.xpath('.//a[contains(@class, "author")]/@href').get()# 用户URL authorName_list.append(author_name) likeNr_list.append(like_nr) URL_list.append(url) userURL_list.append(user_url) time.sleep(0.35) except: pass return True authorName_list, likeNr_list, URL_list, userURL_list = [], [], [], [] qbar = tqdm(total=num, desc="已获取的笔记数量...") # 检查是否已经爬取足够数量的笔记,或是否已经达到页面底部 while len(URL_list) < num: if '- THE END -' in browser.page_source: print(f"当前与{key_word}有关的笔记数量少于 {num}") print('检查时间:',time.ctime()) break parsePage(browser.page_source, authorName_list, likeNr_list, URL_list, userURL_list, num) qbar.update(1) if len(URL_list) < num: browser.execute_script('window.scrollTo(0,document.body.scrollHeight)')# 模拟鼠标滚动 time.sleep(random.uniform(3, 5)) if len(URL_list) > num: URL_list = URL_list[:num] authorName_list = authorName_list[:num] likeNr_list = likeNr_list[:num] userURL_list = userURL_list[:num] qbar.close() 这段代码发生了以下错误,请帮我改正 LookupError Traceback (most recent call last) Cell In[11], line 57 54 print('检查时间:',time.ctime()) 55 break ---> 57 parsePage(browser.page_source, authorName_list, likeNr_list, URL_list, userURL_list, num) 58 qbar.update(1) 60 if len(URL_list) < num: Cell In[11], line 17, in parsePage(html_content, authorName_list, likeNr_list, URL_list, userURL_list, num) 1 def parsePage(html_content, authorName_list, likeNr_list, URL_list, userURL_list, num): 2 """ 3 解析网页内容并更新数据列表。 4 (...) 15 None: 数据存储在传入的列表中 16 """ ---> 17 response = Selector(text=html_content) 18 divs = response.xpath('//div[contains(@class, "feeds-container")]/section/div')# 选中网页中包含笔记信息的部分 20 # 遍历divs获取每一篇笔记的信息 File d:\anaconda3\Lib\site-packages\scrapy\selector\unified.py:97, in Selector.__init__(self, response, text, type, root, **kwargs) 94 if root is not _NOT_SET: 95 kwargs["root"] = root ---> 97 super().__init__(text=text, type=st, **kwargs) File d:\anaconda3\Lib\site-packages\parsel\selector.py:496, in Selector.__init__(self, text, type, body, encoding, namespaces, root, base_url, _expr, huge_tree) 493 msg = f"text argument should be of type str, got {text.__class__}" 494 raise TypeError(msg) --> 496 root, type = _get_root_and_type_from_text( 497 text, 498 input_type=type, 499 base_url=base_url, 500 huge_tree=huge_tree, 501 ) 502 self.root = root 503 self.type = type File d:\anaconda3\Lib\site-packages\parsel\selector.py:377, in _get_root_and_type_from_text(text, input_type, **lxml_kwargs) 375 assert input_type in ("html", "xml", None) # nosec 376 type = _xml_or_html(input_type) --> 377 root = _get_root_from_text(text, type=type, **lxml_kwargs) 378 return root, type File d:\anaconda3\Lib\site-packages\parsel\selector.py:329, in _get_root_from_text(text, type, **lxml_kwargs) 326 def _get_root_from_text( 327 text: str, *, type: str, **lxml_kwargs: Any 328 ) -> etree._Element: --> 329 return create_root_node(text, _ctgroup[type]["_parser"], **lxml_kwargs) File d:\anaconda3\Lib\site-packages\parsel\selector.py:110, in create_root_node(text, parser_cls, base_url, huge_tree, body, encoding) 107 body = text.strip().replace("\x00", "").encode(encoding) or b"<html/>" 109 if huge_tree and LXML_SUPPORTS_HUGE_TREE: --> 110 parser = parser_cls(recover=True, encoding=encoding, huge_tree=True) 111 root = etree.fromstring(body, parser=parser, base_url=base_url) 112 else: File d:\anaconda3\Lib\site-packages\lxml\html\__init__.py:1887, in HTMLParser.__init__(self, **kwargs) 1886 def __init__(self, **kwargs): -> 1887 super().__init__(**kwargs) 1888 self.set_element_class_lookup(HtmlElementClassLookup()) File src\\lxml\\parser.pxi:1806, in lxml.etree.HTMLParser.__init__() File src\\lxml\\parser.pxi:858, in lxml.etree._BaseParser.__init__() LookupError: unknown encoding: 'b'utf8''
最新发布
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值