显示来自NSMutableString的特定数据
我有NSMutableString
这个信息:
T: Testadata(id:1,title:"Test",subtitle:"test is correct",note:"second test",identifiers:(
这是我的表执行:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"TestCell"; TestCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell =[ [[NSBundle mainBundle] loadNibNamed:@"TestCell" owner:nil options:nil] lastObject]; } NSArray *array = [server get_test:10 offset:0 sort_by:0 search_for:@""]; NSMutableString *t = [NSMutableString stringWithFormat:@"%@ ", [array objectAtIndex:indexPath.row]]; cell.testTitle.text = t; NSLog(@" T :%@ ", t); return cell; }
现在我所有的testing数据都在我的t中,我想在我的行中显示,只是标题我怎么能在我的标题标签中显示我的标题而不是整个Testdata?在这个实现中,请你帮我一下吗?
该string是调用对象的description
方法的结果
[array objectAtIndex:indexPath.row]
这是服务器返回的对象之一,并且似乎是由服务器API返回的特殊类的对象。
我不知道你正在使用什么服务器API, get_test:
方法返回什么样的对象,但通常有访问器方法来获取从服务器检索到的对象的属性。
首先将对象转换为string(使用stringWithFormat:
,然后尝试从string中提取单个属性是非常麻烦且容易出错的。 如果可能的话,你应该使用适当的访问器方法。
编辑:你现在告诉你使用Thrift API。 我没有这个API的经验,但从快速浏览文档看来,服务器调用返回模型类Testadata
的对象数组。 因此,类似的东西应该是可能的:
Testadata *td = [array objectAtIndex:indexPath.row]; cell.testTitle.text = td.title; cell.testSubtitle.text = td.subtitle;
另一个说法:获取cellForRowAtIndexPath
的对象是非常低效的,因为该方法被频繁调用。 最好只取一次对象(例如在viewDidLoad
)。
你的get_test
方法应该返回一个字典数组。 然而,由于某些原因,你坚持使用string作为testing数据的方法,那么这就是其中之一 –
NSString *titleSting = nil; NSString *testdata = @"Testadata(id:1,title:\"Test\",subtitle:\"test is correct\",note:\"second test\",identifiers:("; NSLog(@"T: %@", testdata); NSArray *components = [testdata componentsSeparatedByString:@","]; for (NSString *aComponent in components) { if ([aComponent hasPrefix:@"title"]) { NSArray *subComponents = [aComponent componentsSeparatedByString:@":"]; titleSting = [subComponents[1] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\""]]; break; } } NSLog(@"titleSting = %@", titleSting);
上述代码的逻辑: – 将原/长string分解为逗号(,)周围的string数组。 然后search“标题”(键),假定它将被放在原始string的逗号(,)后面。 所有的键值对都遵循这个模式key:"value"
。
PS – 这假定'标题'string本身不会有逗号(,)和冒号(:)字符在里面。