2013-04-02 22 views
0

我正在做一個家庭作業,我必須從NSInputstream接收傳入的消息,並且必須在UItableview中查看它,我可以收到消息並正確發送消息,但有一段時間即使我在\ n中輸入,我的字符串仍在同一行中寫入。如何使UItableview與格式長度

做任何人有想法????

例如:
@abc:123:123:123:123 @
@abc:123:123:123:123 @
@abc:1:2:3 @

第一線打印在第一行但在第二行打印第三行也可以告訴我如何做一個格式,如果字符串開始和結束與「@」將寫入每行不在同一行。

我做了很多過濾器的消息,但它無法

我的代碼:

case NSStreamEventHasBytesAvailable: 

     if (theStream == inputStream) { 

      uint8_t buffer[1024]; 
      int len; 

      while ([inputStream hasBytesAvailable]) { 
       len = [inputStream read:buffer maxLength:sizeof(buffer)]; 
       if (len > 0) { 

        NSString *output = [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]; 

        if (nil != output) { 
         NSLog(@"server said: %@", output); 
        } 
       } 
      } 
     } 
     break; 
NSIndexPath *topIndexPath = [NSIndexPath indexPathForRow:messages.count-1 inSection:0]; 
[self.tView scrollToRowAtIndexPath:topIndexPath 
        atScrollPosition:UITableViewScrollPositionMiddle 
          animated:YES]; 

輸出:
@abc:123:123:123:123 @
@abc: 123:123:123:123 @ @abc:1:2:3 @

但是笏我期待:

@abc:123:123:123:123 @
@abc:123:123:123:123 @
@abc:1:2:3 @

....如果字符串開始並以「@」結尾必須寫在下一行...問題是因爲一些時間字符串很小,所以它需要相同的行,但我希望它在下一行,如果開始和結束「@」

+0

可以包括預期的輸出,你會得到什麼現在呢? –

+0

我更新我的問題,根據您的問題..你有什麼想法?我做了這一切,這是我必須完成的最後一件事 – user2234869

回答

0

您可以通過成分分離使用數組..

NSString* string = @"@abc:123:123:123:[email protected]@abc:123:123:123:[email protected]:123:123:[email protected]:1:2:[email protected]:123:123:[email protected]:123:123:123:[email protected]@abc:123:123:123:[email protected] 123:123:123:[email protected]:1:2:[email protected] @abc:123:123:123:[email protected]@abc:123:123:123:[email protected]:123:123:[email protected]:1:2:[email protected] 123:123:123:[email protected]:123:123:123:[email protected]:123:123:[email protected]:123:123:123:[email protected]:123:123:[email protected]:1:2:[email protected]"; 

NSMutableString* formatString = [NSMutableString string]; 

NSArray* strArr = [string componentsSeparatedByString:@"@"]; 
for (NSString* str in strArr) 
{ 
    [formatString appendFormat:@"%@@\[email protected]",str]; 
} 
NSLog(@"%@",formatString);