我有一个UITableView
5 UITableViewCells
.每个单元格包含一个UIButton
设置如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *identifier = @"identifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
[cell autorelelase];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 5, 40, 20)];
[button addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTag:1];
[cell.contentView addSubview:button];
[button release];
}
UIButton *button = (UIButton *)[cell viewWithTag:1];
[button setTitle:@"Edit" forState:UIControlStateNormal];
return cell;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:在buttonPressedAction:
方法中,我如何知道按下了哪个按钮.我考虑过使用标签,但我不确定这是最好的路线.我希望能够以某种方式标记indexPath
到控件上.
- (void)buttonPressedAction:(id)sender
{
UIButton *button = (UIButton …
Run Code Online (Sandbox Code Playgroud) 我需要在合并字符串之前搜索一些字符串并设置一些属性,因此使用NSStrings - >连接它们 - >使NSAttributedString不是一个选项,有没有办法将attributedString连接到另一个referencedString?
我想在字典中的所有键上映射一个函数.我希望像下面这样的东西可以工作,但过滤器不能直接应用于字典.实现这一目标的最简洁方法是什么?
在这个例子中,我试图将每个值递增1.但这对于示例来说是偶然的 - 主要目的是弄清楚如何将map()应用于字典.
var d = ["foo" : 1, "bar" : 2]
d.map() {
$0.1 += 1
}
Run Code Online (Sandbox Code Playgroud) 有没有一种方法来检查字符串nil
和""
斯威夫特?在Rails中,我可以blank()
用来检查.
我目前有这个,但它似乎有点矫枉过正:
if stringA? != nil {
if !stringA!.isEmpty {
...blah blah
}
}
Run Code Online (Sandbox Code Playgroud) 我正在给一个文本视图来推特一些字符串.
我正在应用以下方法将字符数限制为140.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if([text isEqualToString:@"\b"]){
DLog(@"Ohoooo");
return YES;
}else if([[textView text] length] > 140){
return NO;
}
return YES;
}
Run Code Online (Sandbox Code Playgroud)
除了退格不起作用的第一个条件之外,代码运行良好.假设我已达到140个字符的限制,因此该方法将给我假,用户无法插入更多字符,但在此之后,当我尝试删除某些字符时,文本视图的行为与禁用时相同.
那么问题是如何从textview.text
文本视图中删除字符或重新启用文本视图.
我怎样才能在Swift中收集CGFloat?我试过ceil(CDouble(myCGFloat))
但只适用于iPad Air和iPhone 5S.
当在另一个模拟设备上运行时,我收到错误消息 'NSNumber' is not a subtype of 'CGFloat'
我试图将一个对象数组减少到Swift中的一个集合,这是我的代码:
objects.reduce(Set<String>()) { $0.insert($1.URL) }
Run Code Online (Sandbox Code Playgroud)
但是,我收到一个错误:
Type of expression is ambiguous without more context.
Run Code Online (Sandbox Code Playgroud)
我不明白问题是什么,因为URL的类型绝对是String.有任何想法吗?
我只想在Swift中获取单个字符串的ASCII值.这就是我目前正在做的事情:
var singleChar = "a"
println(singleChar.unicodeScalars[singleChar.unicodeScalars.startIndex].value) //prints: 97
Run Code Online (Sandbox Code Playgroud)
这太难看了.必须有一个更简单的方法.
在设置内部 - >常规 - >文本大小,更改文本大小后,我必须退出自己的应用程序才能应用大小
[UIFont preferredFontForTextStyle:..]
Run Code Online (Sandbox Code Playgroud)
是否有代表或通知通知我的应用重新应用新尺寸?
更新:我尝试了以下但有趣的是,字体大小将在我BG之后应用并启动应用程序TWICE.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fromBg:) name:UIApplicationDidBecomeActiveNotification object:nil];
}
-(void) fromBg:(NSNotification *)noti{
self.headline1.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline];
self.subHeadline.font = [UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline];
self.body.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
self.footnote.font = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote];
self.caption1.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption1];
self.caption2.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2];
// [self.view layoutIfNeeded];
}
Run Code Online (Sandbox Code Playgroud)