Chyba jesteście zaznajomieni z Wyrażenia regularne do wykrywania adresów URL, tak, aby uzyskać jeden lub drugi rodzaj widoku w swojej komórce, można po prostu wrócić dwa różne UITableViewCell
s od wybranej metody tableView:cellForRowAtIndexPath:
.
mogłoby to wyglądać tak (proszę zauważyć, wpisane w przeglądarce nie sprawdzone)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *dataString = // Get your string from the data model
// Simple pattern found here: http://regexlib.com/Search.aspx?k=URL
NSString *URLpattern = @"^http\\://[a-zA-Z0-9\-\.]+\\.[a-zA-Z]{2,3}(/\\S*)?$";
NSError *error = NULL;
NSRegularExpression *URLregex = [NSRegularExpression regularExpressionWithPattern:URLpattern
options:NSRegularExpressionCaseInsensitive
error: &error];
NSUInteger numberOfMatches = [URLregex numberOfMatchesInString:string
options:0
range:NSMakeRange(0, [string length])];
if (numberOfMatches == 0) {
static NSString *PlainCellIdentifier = @"PlainCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
cell.textLabel.text = timeZoneWrapper.localeName;
}
else {
static NSString *FancyCellIdentifier = @"FancyCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}
// Configure cell view with text view here
}
return cell;
}
Może to oznaczać "http" w NSString, jeśli zawiera, to jest link. – rishi
@rishi, problem polega na tym, że istnieje wiele typów adresów URL, UITextView wydaje się je wszystkie wykrywać, więc google.com, www.google.com, ... –