我想在SectionIndex列和單元格內容視圖之間畫一條垂直線。我怎樣才能做到這一點?在表格視圖中畫一條垂直線
1
A
回答
3
參考這個帖子:
how to add vertical lines to a table view in iphone sdk?
參考克里斯·馬克爾的沒有在上面的帖子。這表明此鏈接:
http://www.iphonedevx.com/?p=153
注:我從整個鏈接複製的代碼,並粘貼在這裏呢,只是爲了確保答案仍然即使在情況下非常有用,如果鏈路出現故障以後:
UITableView可能是iPhone上最常用的視圖。它的靈活性是 ,用戶界面非常適合在iPhone上使用。 有很多關於如何將多個項目添加到UITableViewCell的示例。 但是,我需要在更傳統的 電子表格樣式網格中呈現一些數據。結果運行良好,並使我能夠在屏幕上包裝 很多信息,這是非常難以遵循 沒有垂直網格。我將在這裏展示一個非常簡化的版本 ,您可以使用它向您的UITableView添加垂直線。
首先我們需要創建一個UITableViewCell的子類。這是因爲我們 可以覆蓋drawrect並繪製我們的線並添加一個數組來保存我們將繪製線條的位置列表。
@interface MyTableCell : UITableViewCell {
NSMutableArray *columns;
}
- (void)addColumn:(CGFloat)position;
@end
在這個簡化的例子中,我們將離開在的UITableViewController細胞實際 文本的位置,並將其放置手動 (完整的源代碼被附加在末尾)。我們只是提供一個 機制來繪製垂直線來製作網格。列位置 通過調用addColumn補充說:
- (void)addColumn:(CGFloat)position {
[columns addObject:[NSNumber numberWithFloat:position]];
}
現在讓我們覆蓋的drawRect。其中我們抓取當前圖形上下文 並設置線條的顏色和寬度。然後我們迭代我們的列 數組,從單元格行的頂部向下繪製一行,並在 的每個位置存儲數組。
- (void)drawRect:(CGRect)rect {
CGContextRef ctx = UIGraphicsGetCurrentContext();
// Use the same color and width as the default cell separator for now
CGContextSetRGBStrokeColor(ctx, 0.5, 0.5, 0.5, 1.0);
CGContextSetLineWidth(ctx, 0.25);
for (int i = 0; i < [columns count]; i++) {
CGFloat f = [((NSNumber*) [columns objectAtIndex:i]) floatValue];
CGContextMoveToPoint(ctx, f, 0);
CGContextAddLineToPoint(ctx, f, self.bounds.size.height);
}
CGContextStrokePath(ctx);
[super drawRect:rect];
}
To add columns to the view just call
[cell addColumn:50];
when you’re building each cell.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *MyIdentifier = [NSString stringWithFormat:@"MyIdentifier %i", indexPath.row];
MyTableCell *cell = (MyTableCell *)[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil) {
cell = [[[MyTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyIdentifier] autorelease];
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0.0, 0, 30.0,
tableView.rowHeight)] autorelease];
[cell addColumn:50];
label.tag = LABEL_TAG;
label.font = [UIFont systemFontOfSize:12.0];
label.text = [NSString stringWithFormat:@"%d", indexPath.row];
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
label = [[[UILabel alloc] initWithFrame:CGRectMake(60.0, 0, 30.0,
tableView.rowHeight)] autorelease];
[cell addColumn:120];
label.tag = VALUE_TAG;
label.font = [UIFont systemFontOfSize:12.0];
// add some silly value
label.text = [NSString stringWithFormat:@"%d", indexPath.row * 4];
label.textAlignment = UITextAlignmentRight;
label.textColor = [UIColor blueColor];
label.autoresizingMask = UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleHeight;
[cell.contentView addSubview:label];
}
return cell;
}
希望這有助於。
2
我在cellForRowAtIndexPath中使用了下面的代碼,這是完美的工作。
static NSString *reuse = @"reuse";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
cell.textLabel.font = [UIFont boldSystemFontOfSize:12.0f];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
}
[cell.contentView setBounds:CGRectMake(cell.bounds.origin.x, cell.bounds.origin.y, 222,60)];
NSString *year = [self.keys objectAtIndex:[indexPath section]];
NSArray *movieSection = [[self.dataAllArtists objectForKey:year] valueForKey:@"row"];
NSDictionary *performanceDic = [movieSection objectAtIndex:[indexPath row]];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = [NetworkData parseString:[performanceDic valueForKey:@"performance_name"]];
int x;
switch ([genreId intValue])
{
case 1:
x=cell.contentView.bounds.size.width+19;
break;
case 3:
x=cell.contentView.bounds.size.width+26;
break;
case 6:
x=cell.contentView.bounds.size.width+16;
break;
case 7:
x=cell.contentView.bounds.size.width+28;
break;
default:
break;
}
for(UIView *view in [tableView subviews])
{
if([[[view class] description] isEqualToString:@"UITableViewIndex"])
{
[view setBackgroundColor:[UIColor whiteColor]];
}
}
[cell.textLabel setLineBreakMode:UILineBreakModeWordWrap];
[cell.textLabel setNumberOfLines:0];
UIView *lineView = [[[UIView alloc] initWithFrame:CGRectMake(x,0,1, cell.contentView.bounds.size.height+2)] autorelease];
lineView.backgroundColor = [UIColor lightGrayColor];
[cell.contentView addSubview:lineView];
return cell;
相關問題
- 1. 畫一條垂直線
- 2. 在Core Plot中畫一條垂直線?
- 3. 在wpf RichTextBox上畫一條垂直線
- 4. 在視頻捕捉中畫一條垂直線opencv
- 5. 在谷歌地圖中畫一條垂直於兩點的線
- 6. 畫一條垂直線,當兩個線圖相遇 - SSRS
- 7. 在Android中畫一條垂直線,由相等部分分開
- 8. 在uiviewcontroller的視圖中繪製水平和垂直線條
- 9. 如何在表格中顯示1條垂直線
- 10. 垂直中心多條線
- 11. Android動畫垂直圖像視圖
- 12. 在圖表中繪製垂直線
- 13. WPF網格擴展列表視圖垂直滾動條丟失
- 14. 對於其餘的垂直空間垂直居中一條線
- 15. c#畫一條直線
- 16. 畫一條直線iOS
- 17. 垂直滾動條不在網格中查看WPF中的列表視圖
- 18. 畫線在HBox中和垂直框
- 19. 表格單元格垂直滾動條一直保持禁用
- 20. 如何在一條線上垂直居中放置圖像?
- 21. 垂直滾動條動畫
- 22. 在列表視圖控件中繪製水平和垂直線
- 23. 畫布垂直指向線
- 24. 垂直線不能畫出
- 25. 如何在表格中添加垂直滾動條(剃鬚刀視圖)
- 26. iPhone垂直視圖控制器動畫
- 27. 在路線圖中畫一條線
- 28. 條形圖上的垂直線
- 29. 延長chart.js水平條形圖以包括一條垂直線
- 30. 帶CSS的垂直線條
@Princekumar:請參閱我的回答,看看它是否可以幫助你。 – 2012-04-09 07:42:23
謝謝!它幫了我很多。 – Warewolf 2012-04-10 04:11:32
@Princekumar:我的答案沒有滾動表視圖。它來自第一次顯示錶格。接受答案,如果它可以幫助你。 – 2012-04-11 04:23:52