我有一个名为delegate.allSelectedVerseEnglish的NSMutableArray来自应用程序委托,它有一些值,我能够获得数组计数,我可以在UITableView中正确显示它.但现在我想在textview中显示它.我的UITableView代码是这样的
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [delegate.allSelectedVerseEnglish count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
readCell *cell = (readCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"readCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.label.text = [NSString stringWithFormat:@"%d",indexPath.row+1];
cell.textLabel.text = [NSString stringWithFormat:@" %@",[delegate.allSelectedVerseEnglish objectAtIndex:indexPath.row]];
cell.textLabel.font …Run Code Online (Sandbox Code Playgroud) NSString,NSBool并且NSInteger有很多额外的功能,在需要时很好,但在不必要的时候成为行李.是否有一种轻量级的方法来保持字符串,就像bool和int一样?
NSBool 具有 BOOLNSInteger 具有 int有什么NSString?
我在过去几天一直在研究一些基础知识,并且我意识到我从未真正理解为什么NSString/NSMutableString的传递引用不起作用.
- (void)testing{
NSMutableString *abc = [NSMutableString stringWithString:@"ABC"];
[self testing:abc];
NSLog(@"%@",abc); // STILL ABC
}
-(void)testing:(NSMutableString *)str {
str = [NSMutableString stringWithString:@"HELP"];
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做?我希望我的测试方法能够从main方法操作String.我一直在使用Mutable Arrays,字典等,并且工作正常.感觉奇怪,我从来没有意识到它如何与字符串一起使用.
但是这个值会改变,就像第一个字符串一样
NSMutableString *string1;
NSMutableString *string2;
string1 = [NSMutableString stringWithString: @"ABC"];
string2 = string1;
[string2 appendString: @" HELP"];
NSLog (@"string1 = %@", string1); // ABC HELP
NSLog (@"string2 = %@", string2); // ABC HELP
Run Code Online (Sandbox Code Playgroud) 如何找到搜索到的字符串的实际索引rangeOfString?所以我有这个:
[name rangeOfString: @"Arnold"];
如何检索字符串位置的索引?我需要开头索引和字符串结束的索引.示例会有所帮助.
我有这样的NSString @"0,0,0,0,0,0,1,2012-03-08,2012-03-17".我想要用逗号分隔的值.我想要逗号之前的值并忽略逗号.
我是iPhone的新手,我知道如何用Java做但不知道如何在Objective-C中做.
我的最终目标是改变字体大小,以便各种单词适合相同的矩形区域.我认为sizeWithFont将是一个很好的方法,但我不断得到宽度为0,我很困惑.
NSString *name = @"Test Word";
CGFloat fontSize = 32.0;
UIFont *font = [UIFont fontWithName:@"TimeNewRomanPSMT" size: fontSize];
CGSize sWidth = [name sizeWithFont:font
constrainedToSize:CGSizeMake(fontSize, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
NSLog(@"sWidth = %3.1f; rec.width n= %3.1f ", sWidth.width, rec.width);
Run Code Online (Sandbox Code Playgroud)
我的计划是循环遍历字体大小,直到宽度适合但sWidth.width保持返回0.
克里斯
是否有一个Obj-C函数来大写第一个字符和小写其他字符?
谢谢你的建议
我试图找出可能在同一方法中重新定义NSString的最佳方法.基本上在过去我用它[self goSomewhereVoid]来处理它们以重新定义字符串然后返回到方法.没有离开方法,有没有其他方法可以做到这一点?谢谢.
- (void)someMethod(NSString *)theString {
isNumberOne = YES;
NSString *theString = @"something that changes often";
[self goSomewhereVoid:theString];
isNumberTwo = YES;
NSString *theString = @"time to change something"; // <- possible to redefine this?
[self goSomewhereElseVoid:theString];
...
Run Code Online (Sandbox Code Playgroud) 我动态追加并从NSString中删除子串 -
在特定的操作我附加使用(我在添加新字符串时使用逗号分隔符) -
self.selectedString = [self.selectedString length] < 1 ? newSelectedString
: [self.selectedString stringByAppendingFormat:@",%@",newSelectedString];
Run Code Online (Sandbox Code Playgroud)
删除 - 现在这个逗号在删除字符串时为我创建了问题.目前我使用的解决方案是 -
self.selectedString = [self.selectedString stringByReplacingOccurrencesOfString:newSelectedString
withString:@""];
NSRange rangeSingleComma = [self.selectedString rangeOfString:@","];
NSRange rangeDoubleComma = [self.selectedString rangeOfString:@",,"];
if (rangeSingleComma.location == [self.selectedString length] - 1) {
self.selectedString = [self.selectedString substringToIndex:[self.selectedString length] - 1];
}
if (rangeSingleComma.location == 0) {
self.selectedString = [self.selectedString substringFromIndex:1];
}
if (rangeDoubleComma.location != NSNotFound) {
self.selectedString = [self.selectedString stringByReplacingOccurrencesOfString:@",,"
withString:@","];
}
Run Code Online (Sandbox Code Playgroud)
但这是一个非常肮脏的方法,任何人都可以提出一个很好的方法.
我有一个NSString我想{在它的开头添加一个.
NSMutableString *str = [[NSMutableString alloc] initWithString:@"{"];
[str stringByAppendingString:extracted];
Run Code Online (Sandbox Code Playgroud)
这str仅返回{.这是为什么?Objective-C非常令人沮丧,因为它提供了许多方法来做同样的事情,但对于情况,需要采用不同的方式.
我尝试做[NSMutableString string]并添加{然后extracted它仍然无法正常工作.为什么这不起作用,我该怎么办?