滚动后自定义 UITableViewCell 按钮标题不再可见(重用)
Custom UITableViewCell button title no longer visible after scrolling (reuse)
故事板的动态原型自定义单元格包含一个系统 UIButton
。
我在 cellForRowAtIndexPath
中设置了按钮的标题:
NSInteger votesCount = verse.helpfulVotesCount.integerValue;
NSString *votes = [NSString stringWithFormat:@"%ld helpful vote%@", (unsigned long)votesCount, votesCount == 1 ? @"" : @"s"];
[cell.detailButton setTitle:votes forState:UIControlStateNormal];
一开始一切都很好,直到滚动单元格 off-screen。那时,重复使用的单元格的按钮标题不再可见。
我检查过的内容:
自定义单元格按钮的属性,包括其 frame
。它的位置和大小是正确的。
按钮的 state
、titleForState
和 titleColorForState
。它有标题和标题颜色,但标题不再可见。
我尝试过的:
正在将按钮类型从系统更改为自定义。按钮标题仍然消失。
设置按钮的背景颜色。背景出现,标题的高度和宽度正确,但标题本身不可见。
为什么滚动后按钮标题消失了?
更新:
- 如果我将每个单元格的按钮标题设置为相同的字符串,滚动后标题仍然可见。只有当新标题与以前的标题不同时,按钮标题才会明显消失。
我认为你在不知情的情况下混合了单元格样式。
没有代码可供我们查看...
所以让我展示一种解决方法...
仅通过一种单元格样式和使用标签。
(您可以使用多个单元格样式,但每个单元格样式有不同的
dequeueReusableCellWithIdentifier)
我正在使用动态原型单元格
单元格样式设置为自定义
我试过和你一样的布局
我给有问题的 按钮 标记了 201
我也在使用 helpfulVotes label 来显示该行。
在IB中,我给它打了个tag 202
一切都在故事板中创建。
我只是在 cellForRowAtIndexPath:
中使用引用
我还创建了一个@IBAction doSomething,
以证明我们可以跟踪点击了哪个按钮。
图片link:http://tinypic.com/r/1yl1l4/8
- (IBAction)doSomething:(UIButton*)sender {
NSLog(@"Button row clicked: %ld",(long)sender.titleLabel.tag);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
// Configure the cell...
NSInteger votesCount = indexPath.row;
NSString *votes =
[NSString stringWithFormat:@"%ld helpful vote%@",
(unsigned long)votesCount, votesCount == 1 ? @"" : @"s"];
UILabel *helpfulVotes = (UILabel *)[cell viewWithTag:202];
[helpfulVotes setText:votes];
UIButton *detailButton = (UIButton *)[cell viewWithTag:201];
[detailButton setTitle:votes forState:UIControlStateNormal];
//save indexPath.row inside this tag
detailButton.titleLabel.tag = indexPath.row;
return cell;
}
Swift 版本,类似于俚语 Objective-C:
@IBAction func doSomething(sender: UIButton) {
if let tag = sender.titleLabel?.tag {
println("row:\(tag)")
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
var someTitle = cell.viewWithTag(202) as UILabel
someTitle.text = "Row \(indexPath.row):00"
var detailButton = cell.viewWithTag(201) as UIButton
//save indexPath.row inside this tag
detailButton.titleLabel?.tag = indexPath.row
detailButton.setTitle("Button:\(indexPath.row)",
forState: UIControlState.Normal)
return cell
}
故事板的动态原型自定义单元格包含一个系统 UIButton
。
我在 cellForRowAtIndexPath
中设置了按钮的标题:
NSInteger votesCount = verse.helpfulVotesCount.integerValue;
NSString *votes = [NSString stringWithFormat:@"%ld helpful vote%@", (unsigned long)votesCount, votesCount == 1 ? @"" : @"s"];
[cell.detailButton setTitle:votes forState:UIControlStateNormal];
一开始一切都很好,直到滚动单元格 off-screen。那时,重复使用的单元格的按钮标题不再可见。
我检查过的内容:
自定义单元格按钮的属性,包括其
frame
。它的位置和大小是正确的。按钮的
state
、titleForState
和titleColorForState
。它有标题和标题颜色,但标题不再可见。
我尝试过的:
正在将按钮类型从系统更改为自定义。按钮标题仍然消失。
设置按钮的背景颜色。背景出现,标题的高度和宽度正确,但标题本身不可见。
为什么滚动后按钮标题消失了?
更新:
- 如果我将每个单元格的按钮标题设置为相同的字符串,滚动后标题仍然可见。只有当新标题与以前的标题不同时,按钮标题才会明显消失。
我认为你在不知情的情况下混合了单元格样式。
没有代码可供我们查看...
所以让我展示一种解决方法...
仅通过一种单元格样式和使用标签。
(您可以使用多个单元格样式,但每个单元格样式有不同的
dequeueReusableCellWithIdentifier)
我正在使用动态原型单元格
单元格样式设置为自定义
我试过和你一样的布局
我给有问题的 按钮 标记了 201
我也在使用 helpfulVotes label 来显示该行。
在IB中,我给它打了个tag 202
一切都在故事板中创建。
我只是在 cellForRowAtIndexPath:
我还创建了一个@IBAction doSomething,
以证明我们可以跟踪点击了哪个按钮。
图片link:http://tinypic.com/r/1yl1l4/8
- (IBAction)doSomething:(UIButton*)sender {
NSLog(@"Button row clicked: %ld",(long)sender.titleLabel.tag);
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
// Configure the cell...
NSInteger votesCount = indexPath.row;
NSString *votes =
[NSString stringWithFormat:@"%ld helpful vote%@",
(unsigned long)votesCount, votesCount == 1 ? @"" : @"s"];
UILabel *helpfulVotes = (UILabel *)[cell viewWithTag:202];
[helpfulVotes setText:votes];
UIButton *detailButton = (UIButton *)[cell viewWithTag:201];
[detailButton setTitle:votes forState:UIControlStateNormal];
//save indexPath.row inside this tag
detailButton.titleLabel.tag = indexPath.row;
return cell;
}
Swift 版本,类似于俚语 Objective-C:
@IBAction func doSomething(sender: UIButton) {
if let tag = sender.titleLabel?.tag {
println("row:\(tag)")
}
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
var someTitle = cell.viewWithTag(202) as UILabel
someTitle.text = "Row \(indexPath.row):00"
var detailButton = cell.viewWithTag(201) as UIButton
//save indexPath.row inside this tag
detailButton.titleLabel?.tag = indexPath.row
detailButton.setTitle("Button:\(indexPath.row)",
forState: UIControlState.Normal)
return cell
}