在 table 视图中添加动态图片
Dynamic Pictures Adding in table View
我正在尝试为我做一些棘手的事情(布局约束的新手)。
我的想法是,我必须将图片动态添加到 table cell.The 单元格结构是:
- 图像视图
- 文本视图
- 滚动视图(水平滚动)
其中包含图像部分和数字中的图像并不总是存在,也不是恒定的(一些单元格包含图像和文本,而其他单元格仅包含文本)这就是为什么我需要以编程方式添加约束。
我已经搜索了很多东西并应用了很多东西,但现在我遇到了异常并且应用程序崩溃了。希望你能帮助我。
这是我的代码:
var imgsScrollView = UIScrollView()
cell.employmentEventTextView.text = employmentEventsList[indexPath.row].body
if (imgsCountintheCell == 1){
eventsListView.rowHeight = 220
}else{
eventsListView.rowHeight = 500
}
for var i = 0 ; i < imgsCountintheCell ; i++ {
if(i == 0){
var int : String = String(i)
//getting the image from URL Block
var imgName = "image" + int
let url = NSURL(string: employmentEventsList[indexPath.row].imgs[i])
let data = NSData(contentsOfURL: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check
let image = UIImage(data: data!)
let imgView = UIImageView(image: image!)
imgView.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
imgView.setTranslatesAutoresizingMaskIntoConstraints(false)
var constX = NSLayoutConstraint(item: imgView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
var constTop = NSLayoutConstraint(item: imgView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: cell.employmentEventTextView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 2)
var textPlaceBottom = NSLayoutConstraint(item: cell.employmentEventTextView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: imgView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 2)
eventsListView.addSubview(imgView)
imgView.addConstraint(constX)
imgView.addConstraint(constTop)
cell.employmentEventTextView.addConstraint(textPlaceBottom)
}else{
var int : String = String(i)
//getting the image from URL Block
var imgName = "image" + int
let url = NSURL(string: employmentEventsList[indexPath.row].imgs[i])
let data = NSData(contentsOfURL: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check
let image = UIImage(data: data!)
let imgView = UIImageView(image: image!)
imgView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
imgView.setTranslatesAutoresizingMaskIntoConstraints(false)
let constscrollViewToTheTop = NSLayoutConstraint(item: imgsScrollView, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: cell.employmentEventTextView , attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 5)
imgsScrollView.addSubview(imgView)
eventsListView.addSubview(imgsScrollView)
imgsScrollView.addConstraint(constscrollViewToTheTop)
}
我的例外代码是:
Assertion failure in -[UIImageView nsli_lowerAttribute:intoExpression:withCoefficient:forConstraint:], /SourceCache/UIKit_Sim/UIKit-3318.93/NSLayoutConstraint_UIKitAdditions.m:3502
2015-05-27 01:49:58.097 طاقات علم[1713:38226] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unexpected use of internal layout attribute'
*** First throw call stack:
(
0 CoreFoundation 0x0000000100cc1a75 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000102819bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000100cc18da +[NSException raise:format:arguments:] + 106
3 Foundation 0x000000010115cb6f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 UIKit 0x0000000101bdea77 -[UIView(AdditionalLayoutSupport) nsli_lowerAttribute:intoExpression:withCoefficient:forConstraint:] + 3483
5 Foundation 0x00000001010e2b9e lower_1_attribute + 158
6 Foundation 0x00000001010e26b4 -[NSLayoutConstraint _lowerIntoExpression:reportingConstantIsRounded:] + 102
7 Foundation 0x00000001010d862e -[NSLayoutConstraint _addToEngine:integralizationAdjustment:mutuallyExclusiveConstraints:] + 96
8 UIKit 0x0000000101bd0cf9 -[UIView(UIConstraintBasedLayout) _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 411
9 UIKit 0x0000000101bd0f94 -[UIView(UIConstraintBasedLayout) _tryToAddConstraintWithoutUpdatingConstraintsArray:roundingAdjustment:mutuallyExclusiveConstraints:] + 30
10 UIKit 0x0000000101bd10bc -[UIView(UIConstraintBasedLayout) _tryToAddConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 243
11 طاقات علم 0x0000000100910764 _TFCX15__ymcamGdqDduaj30EmploymentEventsViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 7892
12 طاقات علم 0x000000010091164f _TToFCX15__ymcamGdqDduaj30EmploymentEventsViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 79
13 UIKit 0x000000010164be03 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 508
14 UIKit 0x000000010162b901 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2846
15 UIKit 0x000000010164178c -[UITableView layoutSubviews] + 213
16 UIKit 0x00000001015ce1c3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
17 QuartzCore 0x00000001054d6c58 -[CALayer layoutSublayers] + 150
18 QuartzCore 0x00000001054cb87e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
19 QuartzCore 0x00000001054cb6ee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
20 QuartzCore 0x000000010543936e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
21 QuartzCore 0x000000010543a482 _ZN2CA11Transaction6commitEv + 390
22 QuartzCore 0x000000010543aaed _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
23 CoreFoundation 0x0000000100bf6507 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
24 CoreFoundation 0x0000000100bf6460 __CFRunLoopDoObservers + 368
25 CoreFoundation 0x0000000100bec293 __CFRunLoopRun + 1123
26 CoreFoundation 0x0000000100bebbc6 CFRunLoopRunSpecific + 470
27 GraphicsServices 0x0000000104dcaa58 GSEventRunModal + 161
28 UIKit 0x0000000101554580 UIApplicationMain + 1282
29 طاقات علم 0x000000010094c19e top_level_code + 78
30 طاقات علم 0x000000010094c27a main + 42
31 libdyld.dylib 0x0000000102ff5145 start + 1
32 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
提前感谢任何帮助
更新:
我注意到有一种方法可以解决我的问题并且它似乎有效,我的想法是我可以使用空白对象的约束概念自动禁用(当图像视图为空时文本视图自动向上移动)我发现me.Easy 和快速解决方案就可以了。
现在我有一个问题要问:有没有办法让相同的 table 具有多个行高值? (例如,第一行和第二行 row.hight = 60 因为它们没有图片,第三行 row.hight = 500 因为它有图片。)
是否可以在同一个单元格中给每个单元格一个单独的高度值table?
我个人觉得为单元格创建一个 nib 文件更容易,然后 'collapse' 如果某些视图应该被隐藏则约束。例如,使用您的 UIImageView、UITextView 和 UIScrollView 以及所有约束创建一个笔尖。为视图和视图的 height/width 约束添加 IBOutlets。然后如果你需要隐藏一个view,设置它的height/width约束常量为0.
请注意,因为你应该回收你的细胞,你需要在 -(void)prepareForReuse
中重置约束常量
您可以通过实现 UITableView 委托方法为每个单元格设置不同的高度(在 Swift 中语法会有所不同,但名称相同)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
对于每一行,您可以 return 不同的高度。
复杂的高度可能会有点棘手,因为有时您必须构建单元格来计算其高度,然后您必须在 cellForRowAtIndexPath
中再次构建它
我正在尝试为我做一些棘手的事情(布局约束的新手)。
我的想法是,我必须将图片动态添加到 table cell.The 单元格结构是:
- 图像视图
- 文本视图
- 滚动视图(水平滚动) 其中包含图像部分和数字中的图像并不总是存在,也不是恒定的(一些单元格包含图像和文本,而其他单元格仅包含文本)这就是为什么我需要以编程方式添加约束。
我已经搜索了很多东西并应用了很多东西,但现在我遇到了异常并且应用程序崩溃了。希望你能帮助我。 这是我的代码:
var imgsScrollView = UIScrollView()
cell.employmentEventTextView.text = employmentEventsList[indexPath.row].body
if (imgsCountintheCell == 1){
eventsListView.rowHeight = 220
}else{
eventsListView.rowHeight = 500
}
for var i = 0 ; i < imgsCountintheCell ; i++ {
if(i == 0){
var int : String = String(i)
//getting the image from URL Block
var imgName = "image" + int
let url = NSURL(string: employmentEventsList[indexPath.row].imgs[i])
let data = NSData(contentsOfURL: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check
let image = UIImage(data: data!)
let imgView = UIImageView(image: image!)
imgView.frame = CGRect(x: 0, y: 0, width: 200, height: 200)
imgView.setTranslatesAutoresizingMaskIntoConstraints(false)
var constX = NSLayoutConstraint(item: imgView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
var constTop = NSLayoutConstraint(item: imgView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: cell.employmentEventTextView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 2)
var textPlaceBottom = NSLayoutConstraint(item: cell.employmentEventTextView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: imgView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 2)
eventsListView.addSubview(imgView)
imgView.addConstraint(constX)
imgView.addConstraint(constTop)
cell.employmentEventTextView.addConstraint(textPlaceBottom)
}else{
var int : String = String(i)
//getting the image from URL Block
var imgName = "image" + int
let url = NSURL(string: employmentEventsList[indexPath.row].imgs[i])
let data = NSData(contentsOfURL: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check
let image = UIImage(data: data!)
let imgView = UIImageView(image: image!)
imgView.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
imgView.setTranslatesAutoresizingMaskIntoConstraints(false)
let constscrollViewToTheTop = NSLayoutConstraint(item: imgsScrollView, attribute: NSLayoutAttribute.Top, relatedBy: .Equal, toItem: cell.employmentEventTextView , attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 5)
imgsScrollView.addSubview(imgView)
eventsListView.addSubview(imgsScrollView)
imgsScrollView.addConstraint(constscrollViewToTheTop)
}
我的例外代码是:
Assertion failure in -[UIImageView nsli_lowerAttribute:intoExpression:withCoefficient:forConstraint:], /SourceCache/UIKit_Sim/UIKit-3318.93/NSLayoutConstraint_UIKitAdditions.m:3502
2015-05-27 01:49:58.097 طاقات علم[1713:38226] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Unexpected use of internal layout attribute'
*** First throw call stack:
(
0 CoreFoundation 0x0000000100cc1a75 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000102819bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000100cc18da +[NSException raise:format:arguments:] + 106
3 Foundation 0x000000010115cb6f -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
4 UIKit 0x0000000101bdea77 -[UIView(AdditionalLayoutSupport) nsli_lowerAttribute:intoExpression:withCoefficient:forConstraint:] + 3483
5 Foundation 0x00000001010e2b9e lower_1_attribute + 158
6 Foundation 0x00000001010e26b4 -[NSLayoutConstraint _lowerIntoExpression:reportingConstantIsRounded:] + 102
7 Foundation 0x00000001010d862e -[NSLayoutConstraint _addToEngine:integralizationAdjustment:mutuallyExclusiveConstraints:] + 96
8 UIKit 0x0000000101bd0cf9 -[UIView(UIConstraintBasedLayout) _layoutEngine_didAddLayoutConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 411
9 UIKit 0x0000000101bd0f94 -[UIView(UIConstraintBasedLayout) _tryToAddConstraintWithoutUpdatingConstraintsArray:roundingAdjustment:mutuallyExclusiveConstraints:] + 30
10 UIKit 0x0000000101bd10bc -[UIView(UIConstraintBasedLayout) _tryToAddConstraint:roundingAdjustment:mutuallyExclusiveConstraints:] + 243
11 طاقات علم 0x0000000100910764 _TFCX15__ymcamGdqDduaj30EmploymentEventsViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 7892
12 طاقات علم 0x000000010091164f _TToFCX15__ymcamGdqDduaj30EmploymentEventsViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 79
13 UIKit 0x000000010164be03 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 508
14 UIKit 0x000000010162b901 -[UITableView _updateVisibleCellsNow:isRecursive:] + 2846
15 UIKit 0x000000010164178c -[UITableView layoutSubviews] + 213
16 UIKit 0x00000001015ce1c3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
17 QuartzCore 0x00000001054d6c58 -[CALayer layoutSublayers] + 150
18 QuartzCore 0x00000001054cb87e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
19 QuartzCore 0x00000001054cb6ee _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
20 QuartzCore 0x000000010543936e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
21 QuartzCore 0x000000010543a482 _ZN2CA11Transaction6commitEv + 390
22 QuartzCore 0x000000010543aaed _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 89
23 CoreFoundation 0x0000000100bf6507 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
24 CoreFoundation 0x0000000100bf6460 __CFRunLoopDoObservers + 368
25 CoreFoundation 0x0000000100bec293 __CFRunLoopRun + 1123
26 CoreFoundation 0x0000000100bebbc6 CFRunLoopRunSpecific + 470
27 GraphicsServices 0x0000000104dcaa58 GSEventRunModal + 161
28 UIKit 0x0000000101554580 UIApplicationMain + 1282
29 طاقات علم 0x000000010094c19e top_level_code + 78
30 طاقات علم 0x000000010094c27a main + 42
31 libdyld.dylib 0x0000000102ff5145 start + 1
32 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
提前感谢任何帮助
更新: 我注意到有一种方法可以解决我的问题并且它似乎有效,我的想法是我可以使用空白对象的约束概念自动禁用(当图像视图为空时文本视图自动向上移动)我发现me.Easy 和快速解决方案就可以了。 现在我有一个问题要问:有没有办法让相同的 table 具有多个行高值? (例如,第一行和第二行 row.hight = 60 因为它们没有图片,第三行 row.hight = 500 因为它有图片。)
是否可以在同一个单元格中给每个单元格一个单独的高度值table?
我个人觉得为单元格创建一个 nib 文件更容易,然后 'collapse' 如果某些视图应该被隐藏则约束。例如,使用您的 UIImageView、UITextView 和 UIScrollView 以及所有约束创建一个笔尖。为视图和视图的 height/width 约束添加 IBOutlets。然后如果你需要隐藏一个view,设置它的height/width约束常量为0.
请注意,因为你应该回收你的细胞,你需要在 -(void)prepareForReuse
中重置约束常量您可以通过实现 UITableView 委托方法为每个单元格设置不同的高度(在 Swift 中语法会有所不同,但名称相同)
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
对于每一行,您可以 return 不同的高度。
复杂的高度可能会有点棘手,因为有时您必须构建单元格来计算其高度,然后您必须在 cellForRowAtIndexPath
中再次构建它