我有一个 UITableView,它应该只在第一部分第一行显示一个 UISwitch,但由于某种原因,开关显示在第四部分。此外,每当打开/关闭开关时,一个新的 UISwitch 就会出现在另一个单元格中。它的行为非常奇怪。任何帮助将不胜感激。
代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
PrettyTableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[PrettyTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.tableViewBackgroundColor = tableView.backgroundColor;
}
[cell prepareForTableView:tableView indexPath:indexPath];
cell.cornerRadius = 10;
if (indexPath.section == 0) {
if (indexPath.row == 0) {
cell.textLabel.text = @"Push Notifications";
pushNotificationSwitch = [[UISwitch alloc] init];
pushNotificationSwitch.on = pushSwitchState;
cell.accessoryView = pushNotificationSwitch;
[pushNotificationSwitch addTarget:self action:@selector(pushSwitch:) forControlEvents:UIControlEventValueChanged];
cell.userInteractionEnabled = YES;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
}
if (indexPath.section == 1) {
if (indexPath.row == 0) {
cell.textLabel.text = @"Department Based";
if (departmentBased) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
cell.userInteractionEnabled = YES;
}
if (indexPath.row == 1) {
cell.textLabel.text = @"Location Based (GPS)";
if (locationBased) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else {
cell.accessoryType = UITableViewCellAccessoryNone;
} cell.userInteractionEnabled = YES;
}
}
if (departmentBased) {
if (indexPath.section == 2) {
if (indexPath.row == 0) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 1) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 2) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 3) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 4) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 5) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 6) {
cell.textLabel.text = @"xxxxxx";
}
}
if (indexPath.section == 3) {
if (indexPath.row == 0) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 1) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 2) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 3) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 4) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 5) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 6) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 7) {
cell.textLabel.text = @"xxxxxx";
}
if (indexPath.row == 8) {
cell.textLabel.text = @"xxxxxx";
}
}
}
return cell;
}
最佳答案
您正在体验 iOS 中单元可重用性的影响。您对所有单元格使用相同的单元格标识符,因此当您滚动时,旧单元格将变得可用并从 tableView 中出列。
尝试在之后立即将 cell.accessoryView 设置为 nil:
if (cell == nil) {
cell = [[PrettyTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.tableViewBackgroundColor = tableView.backgroundColor;
}
cell.accessoryView = nil;
或者,为第一个 indexPath 选择一个不同的单元格标识符,确保在出队时考虑到这一点。
BOOL firstCell = indexPath.row == 0 && indexPath.section == 0;
NSString *CellIdentifier = firstCell ? @"CellWithSwitch" : @"Cell";
PrettyTableViewCell *cell =
[tableViewdequeueReusableCellWithIdentifier: CellIdentifier];
if (cell == nil) {
cell = [[PrettyTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
cell.tableViewBackgroundColor = tableView.backgroundColor;
}
关于iOS UITableView 正在绘制奇怪的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14918309/
我正在尝试用Prawn生成PDF。在我的PDF模板中,我有带单元格的表格。在其中一个单元格中,我有一个电子邮件地址:cell_email=pdf.make_cell(:content=>booking.user_email,:border_width=>0)我想让电子邮件链接到“mailto”链接。我知道我可以这样链接:pdf.formatted_text([{:text=>booking.user_email,:link=>"mailto:#{booking.user_email}"}])但是将这两行组合起来(将格式化文本作为内容)不起作用:cell_email=pdf.make_c
我对单元测试还是比较陌生。我用Ruby编写了一个类,它接受一个文件,在该文件中搜索给定的Regex模式,替换它,然后将更改保存回文件。我希望能够为此方法编写单元测试,但我不知道我将如何去做。有人能告诉我我们如何对处理文件i/o的方法进行单元测试吗? 最佳答案 看看这个HowdoIunit-testsavingfiletothedisk?基本上这个想法是一样的,文件系统是你的类的依赖。所以引入一个可以在你的单元测试中模拟的角色/接口(interface)(这样你在单元测试时就没有依赖性);角色中的方法应该是您从文件系统中需要的所有东西
我完全不是程序员,正在学习使用Ruby和Rails框架进行编程。我目前正在使用Ruby1.8.7和Rails3.0.3,但我想知道我是否应该升级到Ruby1.9,因为我真的没有任何升级的“遗留”成本。缺点是什么?我是否会遇到与普通gem的兼容性问题,或者甚至其他我不太了解甚至无法预料的问题? 最佳答案 你应该升级。不要坚持从1.8.7开始。如果您发现不支持1.9.2的gem,请避免使用它们(因为它们很可能不被维护)。如果您对gem是否兼容1.9.2有任何疑问,您可以在以下位置查看:http://www.railsplugins.or
有没有人用ruby解决这个问题:假设我们有:a=8.1999999我们想将它四舍五入为2位小数,即8.20,然后乘以1,000,000得到8,200,000我们是这样做的;(a.round(2)*1000000).to_i但是我们得到的是8199999,为什么?奇怪的是,如果我们乘以1000、100000或10000000而不是1000000,我们会得到正确的结果。有人知道为什么吗?我们正在使用ruby1.9.2并尝试使用1.9.3。谢谢! 最佳答案 每当你在计算中得到时髦的数字时使用bigdecimalrequire'bi
我有一个模块:moduleMyModuledefdo_something#...endend由类使用如下:classMyCommandextendMyModuledefself.execute#...do_somethingendend如何验证MyCommand.execute调用了do_something?我已经尝试使用mocha进行部分模拟,但是当未调用do_something时它不会失败:it"callsdo_something"doMyCommand.stubs(:do_something)MyCommand.executeend 最佳答案
尝试使用rubytest/test_foo.rb运行minitest单元测试时出现以下错误:Warning:youshouldrequire'minitest/autorun'instead.Warning:oradd'gem"minitest"'before'require"minitest/autorun"'From:/home/emile/.rvm/rubies/ruby-2.0.0-p0/lib/ruby/2.0.0/minitest/autorun.rb:15:```test_foo.rb看起来像这样:require'minitest/autorun'classTestFoo
在Test::Unit中的ruby单元测试断言失败后,在执行teardown之前,是否有一些简洁优雅的方法来立即执行我的代码?我正在做一些自动化的GUI测试,并希望在出现问题后立即截图。 最佳答案 如果您使用的是1.9,请不要使用Test::Unit::TestCase作为您的基类。对其进行子类化并覆盖#run_test以进行救援,截取屏幕截图并重新提出:classMyAbstractTestCase或者,我认为这实际上是最简洁的方法,您可以使用before_teardownHook:classMyTestCase这不适用于1.
我正在使用带有单个“帐户”表的STI模型来保存用户和技术人员的信息(即用户...8)错误:test_the_truth(用户测试):ActiveRecord::StatementInvalid:PGError:ERROR:关系“技术人员”不存在:从“技术人员”中删除...从本质上讲,标准框架不承认Technicians和Users表(或PostgreSQL称它们为“关系”)不存在,事实上,应该别名为Accounts。有什么想法吗?我对RoR比较陌生,不知道如何解决这个问题而又不完全删除STI。 最佳答案 原来问题是由于存在:./te
defreverse(ary)result=[]forresult[0,0]inaryendresultendassert_equal["baz","bar","foo"],reverse(["foo","bar","baz"])这行得通,我想了解原因。有什么解释吗? 最佳答案 如果我使用each而不是for/in重写它,它看起来像这样:defreverse(ary)result=[]#forresult[0,0]inaryary.eachdo|item|result[0,0]=itemendresultendforainb基本上就
为获取当前时间的代码编写单元测试的最佳方法是什么?例如,某些对象可能仅在工作日创建,其他对象在检查执行某些操作的权限时会考虑当前时间等。我想我应该模拟Date.today和Time.now。这是正确的做法吗?更新:两种解决方案(a)Time.is和(b)Time.stubs(:now).returns(t)都有效。(a)是非常好的方法,但(b)解决方案将与其他测试代码更加一致。在此question作者要求一个通用的解决方案。对于Ruby,在我的选项中,上述两个解决方案是simpler因此比提取获取当前日期/时间的代码更好。顺便说一句,我建议使用Chronic获得所需的时间,例如requ