在表格视图中有很多行,当我点击任何表视图行时,它将像上面的示例一样展开。
所以,这样我想显示一些其他数据,我不想导航到下一个视图。
这里的另一点是,在点击特定行后展开,然后在这里我要显示网页视图。在我的项目中,一些数据是某些文本行或可能是网页的消息。 因此,它还必须显示Web视图,并且必须根据数据或Web内容动态调整tableview单元格。
就像那样,如果点击任何一个单元格,它必须展开并显示网页视图。即使有时它不包含任何网页数据也可能包含一些信息,然后它必须显示网页视图。
这是我目前的项目样本。我正在按照以下方式显示数据/信息。 这是我的表格视图(见图1),点击行后会显示一个这样的弹出窗口(见图2) - 它包含一些显示如下的信息,图像3 - 它包含网页数据/网页视图。
这是我的didSelectRowAtIndexPath方法,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSDictionary *finaldic=[mutableArray objectAtIndex:indexPath.row];
[self showWebview:@"" body:[finaldic objectForKey:@"body"] popupStyle:CNPPopupStyleActionSheet];
}
和WebView方法,
-(void)showWebview:(NSString*)tittle body:(NSString*)body popupStyle:(CNPPopupStyle)popupStyle{
NSMutableParagraphStyle *paragraphStyle = NSMutableParagraphStyle.new;
paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
paragraphStyle.alignment = NSTextAlignmentCenter;
NSAttributedString *title = [[NSAttributedString alloc] initWithString:tittle attributes:@{NSFontAttributeName : [UIFont boldSystemFontOfSize:14], NSParagraphStyleAttributeName : paragraphStyle}];
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.numberOfLines = 0;
titleLabel.attributedText = title;
// UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
// customView.backgroundColor = [UIColor hx_colorWithHexString:@"#00aeef"];
// [customView addSubview:titleLabel];
UIWebView *webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,self.view.frame.size.height/2)];
// webview.scalesPageToFit = YES;
webview.autoresizesSubviews = YES;
//webview.delegate=self;
webview.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
NSRange range = [body rangeOfString:@"<body"];
if(range.location != NSNotFound) {
// Adjust style for mobile
float inset = 40;
NSString *style = [NSString stringWithFormat:@"<style>div {max-width: %fpx;}</style>", self.view.bounds.size.width - inset];
body = [NSString stringWithFormat:@"%@%@%@", [body substringToIndex:range.location], style, [body substringFromIndex:range.location]];
}
[webview loadHTMLString:body baseURL:nil];
self.popupController = [[CNPPopupController alloc] initWithContents:@[titleLabel,webview]];
self.popupController.theme = [CNPPopupTheme defaultTheme];
self.popupController.theme.popupStyle = popupStyle;
self.popupController.delegate = self;
[self.popupController presentPopupControllerAnimated:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
}
答案 0 :(得分:1)
检查我的以下代码,它会帮助你..!我是根据你的代码创建的。
这是你的didSelectRowAtIndexPath方法,
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//user taps expnmade view
if(selectedIndex == indexPath.row)
{
selectedIndex =-1;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade ];
return;
}
//user taps diff row
if(selectedIndex != -1)
{
NSIndexPath *prevPath= [NSIndexPath indexPathForRow:selectedIndex inSection:0];
selectedIndex=(int)indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:prevPath, nil] withRowAnimation:UITableViewRowAnimationFade ];
}
//user taps new row with none expanded
selectedIndex =(int)indexPath.row;
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade ];
}
写下这个方法,
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(selectedIndex == indexPath.row)
{
// return 200;
UITableViewCell *cell = [self tableView: tableView cellForRowAtIndexPath: indexPath];
return cell.bounds.size.height;
}
else
{
return 90;
}
}
并在cellForRowAtIndexPath方法中添加此代码
NSDictionary *finaldic=[mutableArray objectAtIndex:indexPath.row];
NSString *body= [finaldic objectForKey:@"body"];
NSRange range = [body rangeOfString:@"<body"];
if(range.location != NSNotFound) {
// Adjust style for mobile
float inset = 40;
NSString *style = [NSString stringWithFormat:@"<style>div {max-width: %fpx;}</style>", self.view.bounds.size.width - inset];
body = [NSString stringWithFormat:@"%@%@%@", [body substringToIndex:range.location], style, [body substringFromIndex:range.location]];
}
[cell.webView loadHTMLString:body baseURL:nil];
我希望,这对你有用..!
答案 1 :(得分:0)
设计两个不同的表格视图单元格很简单。一个是简单的和其他扩展细胞。当您单击简单单元格时,然后更改特定单元格以展开单元格。