Reputation: 41
I have NSMutableString
with this information:
T: Testadata(id:1,title:"Test",subtitle:"test is correct",note:"second test",identifiers:(
and here is my table implementation :
- (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;
}
now I have all Testdata in my t , I want to show, just title in my rows how can I just have my title instead of whole Testdata in my title label?would you please help me in this implementation?
Upvotes: 0
Views: 185
Reputation: 6244
Your get_test
method should return an array of dictionaries. However, for some reasons, you stick with string as your test data approach then this is one of the way -
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);
Logic for above code:- Break the original/long string into an array of strings around comma (,). And then search for 'title' (key) assuming it would be placed immediately after comma (,) in the original string. All key-value pair follow this pattern key:"value"
.
PS - this assumes that the 'title' string itself will not have comma (,) and colon (:) characters inside it.
Upvotes: 1
Reputation: 539805
The string is the result of calling the description
method on the object
[array objectAtIndex:indexPath.row]
which is one of the objects returned by the server, and that seems to be an object of a special class returned by the server API.
I don't know what server API you are
using, and what kind of objects are returned by the get_test:
method, but usually there
are accessor methods to get the properties of the objects retrieved from the server.
Converting the object to a string first (using stringWithFormat:
) and then
trying to extract a single property from the string is cumbersome and error-prone.
If possible, you should use the proper accessor methods instead.
Edit: You have now told that you use the Thrift API. I have no experience
with that API, but from a quick look at the documentation is seems that the server
call returns an array of objects of the model class Testadata
. Therefore something similar to this should be possible:
Testadata *td = [array objectAtIndex:indexPath.row];
cell.testTitle.text = td.title;
cell.testSubtitle.text = td.subtitle;
Another remark: Fetching the objects in cellForRowAtIndexPath
is very inefficient,
because that method is called frequently. It is better to fetch the objects only once
(for example in viewDidLoad
).
Upvotes: 2